help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Union Type Object problem


From: Laurence Finston
Subject: Re: Union Type Object problem
Date: Sun, 8 Aug 2004 17:23:54 +0200 (MEST)

On Sun, 8 Aug 2004, Hans Aberg wrote:

> The problem is this: Suppose D is derived from base B, and you make void*
> conversions on D* and B*, how do you know how to get back a D* or B*, as
> that may vary dynamically. If you do it the proper way, you use B* and a D*
> is converted to a B* via a dynamic_cast<B*>.
>
> You could use a void* if you know that what it represents when you convert
> it back. But the whole idea of polymorphy is that is not available at
> compile time.
>

With respect to my parser, it's generally clear from the rules what
the type of an object is.
I use a `void*' because of the limitations of `union', not because I don't know
the types of the objects in question.
In the cases where the type of an object might be a base or derived class,
I use virtual functions and cast to the base class.
I haven't had to use `dynamic_cast()' in the parser.  So far, I've
always been able to use `static_cast()'.

I've found that it's often more convenient to store information in the object
that I pass as a parameter to `yyparse()', so that sometimes I don't need to set
the semantic value of a rule, or that it's redundant.  This is especially useful
for lists of options.  If I rewrite my parser, or write another one for a
different purpose, I may just use `void*' as the semantic value for simplicity's
sake, unless a single type like `int' would do the job.  Of course, somebody
who's bothered by explicit casts wouldn't like this solution.

Laurence




reply via email to

[Prev in Thread] Current Thread [Next in Thread]