help-bison
[Top][All Lists]
Advanced

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

Re: C++ / multiple instances / iostreams


From: Hans Aberg
Subject: Re: C++ / multiple instances / iostreams
Date: Fri, 23 Jul 2004 19:02:35 +0200

At 11:04 +0200 2004/07/23, Detlef Vollmann wrote:
>> Yes, one builds a hierarchy of classes, all derived from one root class --
>> which I called class object_root. If you want to put a class of any kind
>> into this polymorphic hierarchy, then make a virtual derivation from the
>> root class, like:
>>   class my_class : public virtual object_root {
>>     ...
>>   };
>> Then the class object that maintains a reference-counted pointer to
>>   object_root* data_;
>> also has functions
>>   object_root* data() { return data_; }
>>   const object_root* data() const { return data_; }
>> Thus, a typical Bison .y action might look like:
>>   my_class& mc = dynamic_cast<my_class&>(*$5.object_.data());
>> or
>>   my_class* mcp = dynamic_cast<my_class*>($5.object_.data());
>>   if (mcp == 0)  error();
>Ok.  That's fine.
>
>>
>> I can also build an object class maintianing a pointer to my_class:
>>   class my_object_class {
>>     my_class data_;
>>     ...
>>     object_root* data() { return data_; }
>>     const object_root* data() const { return data_; }
>>     ...
>>   };
>That was the part that was what was not clear to me.
>As long as you don't dynamic_cast between different kinds of
>my_object_class, your code is fine.

This may, by mistake, also happen, but then it shows up as a runtime error.
Because of this, it is probably better to use the dynamic_cast<T&> form, as
the code becomes simpler, and normally will not throw any exceptions. Any
eventual exceptions can be traced with a debugger with such capacity.

  Hans Aberg






reply via email to

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