Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++] Anonymous structs - Constructors?

Started by deadimp Aug 28, 2007 at 8:24 AM 5 replies 16.3k views
Original Post
deadimp
deadimp
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();

Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
SiCrane
SiCrane
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.
swiftcoder
swiftcoder
Quote:
Original post by SiCrane
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.


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).
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
SiCrane
SiCrane
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.
deadimp
deadimp
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.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
swiftcoder
swiftcoder
Quote:
Original post by deadimp
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).


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?
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
Jerax
Jerax
Quote:
Original post by swiftcoder
Quote:
Original post by SiCrane
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.


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.

Topic Locked

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

Sign in to reply to this topic.