public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* embeded anonymous structs and unions...
@ 2003-03-18  1:45 Gareth Pearce
  2003-03-18 23:51 ` Eljay Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Gareth Pearce @ 2003-03-18  1:45 UTC (permalink / raw)
  To: gcc-help

Hi,

I was wondering if there was someway to do the equivelent of what the 
following code looks like...

class Blah
{
    union
    {
        double coords[3];
        struct
        {
            double x, y, z;
        };
    };
}

that is, put an anonymous struct inside an anonymous union...
I tried the above with g++ 3.4 20030314 - with no luck...

the above combined with an operator[] returning a reference to coords 
elements
would allow
Blah c;
c.x = 1;
c.y = 1;
c.z = 1;
for (int i=0; i<3; i++)
    std::cout << c[i] << std::endl;

to function.

Any clues?

Thanks,
Gareth

_________________________________________________________________
MSN Instant Messenger now available on Australian mobile phones. Go to  
http://ninemsn.com.au/mobilecentral/hotmail_messenger.asp

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

* Re: embeded anonymous structs and unions...
  2003-03-18  1:45 embeded anonymous structs and unions Gareth Pearce
@ 2003-03-18 23:51 ` Eljay Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: Eljay Love-Jensen @ 2003-03-18 23:51 UTC (permalink / raw)
  To: Gareth Pearce, gcc-help

Hi Gareth,

Try this...

class Blah {
public:
   double x,y,z;
   double& operator [] (int i) { return (&x)[i]; }
};

Note:  this is relying on the C++ behavior of laying out in memory the 
variables as given.  This is a typical (standard?) behavior of all the C++ 
compilers that I've used.

--Eljay

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

end of thread, other threads:[~2003-03-18 19:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-18  1:45 embeded anonymous structs and unions Gareth Pearce
2003-03-18 23:51 ` 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).