   [C++] Anonymous structs - Constructors?
  I'm just curious, but is there a way to define constructors for anonymous classes?Like defining one for:struct { int x,y; } a;I know that you're able to use inheritance with anonymous structs:struct A {int x;A() { x=0; }void stuff() { printf("A! %d\n",++x);  }};A a;a.stuff();struct : A {int y;//Ctor?void stuff() { printf("Anon! %d %d\n",--x,++y); }} anon;anon.stuff();
  In C++ a struct or class needs to have a name to have a user defined constructor. The compiler will also generate the usual compiler generated constructors (default and copy), but you can't get at them normally.   ;Quote:Original post by SiCraneIn C++ a struct or class needs to have a name to have a user defined constructor. The compiler will also generate the usual compiler generated constructors (default and copy), but you can't get at them normally.And one has to wonder what use they would be, since there is no real way to allocate a new instance of an anonymous class (I guess typeof() could be used - but that is pretty pointless).   ;  Well, the compiler generated defaults would still need to be called if an object containing the anonymous struct were default or copy constructed. Also, you can typedef an anonymous struct. Ex:typedef struct {  int some_data;} S;Though at this point you might as well just name the struct.   ;Quote:And one has to wonder what use they would be, since there is no real way to allocate a new instance of an anonymous class (I guess typeof() could be used - but that is pretty pointless).They would be useful in the sense of delegates in C#, or just functions as objects in &#106avascript, where you can simply assign functions to variables. It'd be useful for a quick callback definition, or maybe just quickly defining a small thread class or something.Just curious. That's all.EDIT: Then again, it'd probably be more aesthetic to use function scoping and actually id the class.I just tested it, and there is a way to instantiate a newly defined struct (though I've only done so using GCC):struct A { virtual void stuff() { printf("Woof!");  } };A* ptr=new (struct : A { void stuff() { printf("Bark!\n"); } });ptr->stuff();Obviously, it's kinda ugly.   ;Quote:Original post by deadimpQuote:And one has to wonder what use they would be, since there is no real way to allocate a new instance of an anonymous class (I guess typeof() could be used - but that is pretty pointless).I just tested it, and there is a way to instantiate a newly defined struct (though I've only done so using GCC):*** Source Snippet Removed ***Obviously, it's kinda ugly.true, but there is still no way to create multiple instances of a anonymous struct, other than defining then when your declare the struct:struct {int a;void func();} inst1, inst2, inst3;or with SiCrane's somewhat redundant typedef. You might as well name the struct in all of these cases, I don't see any advantage to not. On that note, does anyone use anonymous structs in C++ other than to organise data in unions?   ;Quote:Original post by swiftcoderQuote:Original post by SiCraneIn C++ a struct or class needs to have a name to have a user defined constructor. The compiler will also generate the usual compiler generated constructors (default and copy), but you can't get at them normally.And one has to wonder what use they would be, since there is no real way to allocate a new instance of an anonymous class (I guess typeof() could be used - but that is pretty pointless).I've only ever used them as a member struct of another struct or union. Having a free anonymous struct seems like really bad design.   