public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* question about anonymous union usage
@ 2004-08-09 22:54 Jeffrey Holle
  2004-08-10 14:02 ` Eljay Love-Jensen
  0 siblings, 1 reply; 4+ messages in thread
From: Jeffrey Holle @ 2004-08-09 22:54 UTC (permalink / raw)
  To: gcc-help

I'm using g++ v3.4.1.

I'm considering using an anonymous union in the following fashion:

   class SomeClass
   {
     ....
   private:
     bool isPseudo_;
     union {
       LayoutVertex *layoutVertex_;
       PseudoVertex *pseudoVertex_;
     };
   }

Both LayoutVertex and PseudoVertex have the same methods.

Only in the constructors of SomeClass will I have to code in a way that 
differenciates between layoutVertex_ and psuedoVertex_.

My question is can I safely use layoutVertex_ elsewhere even if its 
initialized with a PsuedoVertex object?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: question about anonymous union usage
  2004-08-09 22:54 question about anonymous union usage Jeffrey Holle
@ 2004-08-10 14:02 ` Eljay Love-Jensen
  2004-08-10 14:10   ` Jeffrey Holle
  0 siblings, 1 reply; 4+ messages in thread
From: Eljay Love-Jensen @ 2004-08-10 14:02 UTC (permalink / raw)
  To: jeff.holle, gcc-help

Hi Jeff,

Do you mean use the layoutVertex_ the pointer member variable in SomeClass
elsewhere?

Or use the layoutVertex_ thing-pointed-to (but is really a pseudoVertex_)
elsewhere?

--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: question about anonymous union usage
  2004-08-10 14:02 ` Eljay Love-Jensen
@ 2004-08-10 14:10   ` Jeffrey Holle
  2004-08-10 22:00     ` Eljay Love-Jensen
  0 siblings, 1 reply; 4+ messages in thread
From: Jeffrey Holle @ 2004-08-10 14:10 UTC (permalink / raw)
  To: gcc-help

The later.
I will have isPseudo_ properly set to identify what type of pointer the 
union holds, but I want to ignore it in all SomeClass methods except its 
constructors.

If I do this, there will be times that layoutVertex_ will point to a 
type of PseudoVertex and I'll treat it like a LayoutVertex.

Again, all methods of LayoutVertex and pseudoVertex are the same.

Eljay Love-Jensen wrote:
> Hi Jeff,
> 
> Do you mean use the layoutVertex_ the pointer member variable in SomeClass
> elsewhere?
> 
> Or use the layoutVertex_ thing-pointed-to (but is really a pseudoVertex_)
> elsewhere?
> 
> --Eljay
> 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: question about anonymous union usage
  2004-08-10 14:10   ` Jeffrey Holle
@ 2004-08-10 22:00     ` Eljay Love-Jensen
  0 siblings, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2004-08-10 22:00 UTC (permalink / raw)
  To: jeff.holle, gcc-help

Hi Jeff,

> I will have isPseudo_ properly set to identify what type of pointer the
union holds, but I want to ignore it in all SomeClass methods except its
constructors.

You should check for it in all SomeClass methods, and do things like:

void SomeClass::doDance()
{
  if(isPseudo_) pseudoVertex_->doDance();
  else layoutVertex_->doDance();
};

> If I do this, there will be times that layoutVertex_ will point to a type
of PseudoVertex and I'll treat it like a LayoutVertex.

Then there will be times when the SomeClass object will have a pointer to
PseudoVertex and you'll call the methods as if you had a pointer to
LayoutVertex.

Antics and hijinks ensue.

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-08-10 14:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-09 22:54 question about anonymous union usage Jeffrey Holle
2004-08-10 14:02 ` Eljay Love-Jensen
2004-08-10 14:10   ` Jeffrey Holle
2004-08-10 22:00     ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).