Skip to main content
gamedev.net
🔒 Locked

C++ and annonymous struct

Started by Ryan_001 Apr 4, 2009 at 4:46 PM 6 replies 1.5k views
Original Post
Ryan_001
Ryan_001
I came across a situation where constructing a class along the lines would be convenient.

struct A {
   void* p1;
   void* p2;
   };


struct B {
   union {
      struct {
         void* p1;
         void* p2;
         };
      A a1;
      A a2;
      // ect...
      };
   };

The general idea being a1, a2, ect... can access directly access the data in struct B. Now I can get the same effect with:

struct B {
   union {
      A internalData;
      A a1;
      A a2;
      // ect...
      };
   };

But then access to the internalData members would require some extra typing. I was wondering if there was any way of getting the same effect, without all the extra typing, that doesn't use anonymous structs (as I hear they are non-standard).
Zahlman
Zahlman
I can't imagine what you want this for (especially with more than one A in the same union) or why there are void*'s all over the place. What are you really trying to do?
Ryan_001
Ryan_001
It was probably a poor example. But there are many situations where unions are useful, and they quite often are nicer with anonymous structs. I was wondering if there was any way to 'simulate' anonymous structs in a standard conforming way.

As far as the void*, it could've been any type, void*, int, float, whatever. I guess making them pointers was probably misleading. A more concrete example could be a vector class that would emulate HLSL type functionality, allowing arbitrary swizzles with .xyz type symantics. Something along the lines of:

struct Vector {   union {      struct { float x, y, z, w; };      Swizzle_xzyw xzyw;      Swizzle_zxyw zxyw;      // ect...      };   };


With swizzle's types being defined to return a vector with the values re-arrange. Now obviously it'd be quite easy to define them as functions. I was more wondering hypothetically if anonymous struct type syntax could be done in a conformant manner.
Doggan
Doggan
Quote:
Original post by Telastyn
Quote:
Original post by Ryan_001
But there are many situations where [C++] unions are useful


Such as?


bool isLittleEndian(){	union	{		int a;		char b;	} endianness;	endianness.a = 1;	return endianness.b;}


/devil's advocate
Ryan_001
Ryan_001
Quote:
Original post by Telastyn
Quote:
Original post by Ryan_001
But there are many situations where [C++] unions are useful


Such as?


If I'm not mistaken http://www.gamedev.net/community/forums/topic.asp?topic_id=261920 and http://www.gamedev.net/community/forums/topic.asp?topic_id=230443 both covered situations where they could be useful.
magic_man
magic_man
Quote:
Original post by Doggan
Quote:
Original post by Telastyn
Quote:
Original post by Ryan_001
But there are many situations where [C++] unions are useful


Such as?


bool isLittleEndian(){	union	{		int a;		char b;	} endianness;	endianness.a = 1;	return endianness.b;}


/devil's advocate

This is undefined behaviour and the following is illegal.
Only one member of a union can be active at any one time unless it contains structures and the first member of each structure is the same, then that field can be indexed.
An anonymous union can not define types according to the standard even if MS use it and let you use it.
struct Vector {   union {      struct { float x, y, z, w; };      Swizzle_xzyw xzyw;      Swizzle_zxyw zxyw;      // ect...      };   };
"You insulted me!" I did not say that in the private message Tom Sloper!
Sneftel
Sneftel
Quote:
Original post by Ryan_001
I was wondering if there was any way of getting the same effect, without all the extra typing, that doesn't use anonymous structs (as I hear they are non-standard).
Anonymous structs are de facto standard. As long as you're not specifically telling the compiler to disallow language extensions, you'll be fine.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.