Sign in to follow this  
ankhd

Define a structure with in a class good or bad idea

Recommended Posts

ankhd    2304
Say if I have a class that defines a structure with in it, does every instance of this class define this structure.

E.g.

class a
{

public:

struct somemembers

{
int member1;

int member2;

};


a(void){}

~a(void){}
};


a foo;

a bar; do they both define this structure or because its a structure definition its only defined once.

Share this post


Link to post
Share on other sites
SiS-Shadowman    359
You are defining 2 types, [b]class a[/b] and [b]struct a::somemembers[/b]. However no instance of [b]a[/b] holds an instance of [b]a::somemembers[/b], since there is no "somemembers foo;" within the definition of [b]a[/b]. In my opinion it does make sense to define a type (for example a structure, enum or class) within another type if both belong together.

Share this post


Link to post
Share on other sites
rip-off    10976
The definition of a structure is a compile time concept. An exception is where the compiler needs to emit RTTI for the structure, but even then this will happen once.

Share this post


Link to post
Share on other sites
There are sometimes good reasons to do such a thing (but read SiS-Shadowman's reply, because what you are doing is not what you think you do!). It is for the same reason why one would for example have an enum inside a struct/class.
This allows you for example to write code like [font="Courier New"]File.Open("name", File::readonly);[/font] or [font="Courier New"]vector<sometype>::iterator[/font]. With the internal definitions, it is clear that [font="Courier New"]readonly [/font]refers to a file mode, not for example a memory protection constant, and that [font="Courier New"]iterator [/font]is a class that knows how to deal with a [font="Courier New"]vector [/font](not just [i]some [/i]iterator). The compiler also won't let you feed any random value to [font="Courier New"]Open [/font]either (this can be made to work with any enum, admittedly, but less elegant).

If you want to have an instance of your internal struct with every instance of the parent struct, then you must explicitly add one to the parent struct, or you must make the internal struct unnamed.

Wait, unnamed struct? Does that make sense? Again, yes, even that makes sense sometimes. First, to make some concepts clearer to someone reading your code ("this stuff belongs together"), but also to implement special things.For example, unnamed structs are useful for union or bitfield trickery inside your class. Admittedly not something you do regularly, but sometimes it is useful.

A real life example where I've used this is if you have a struct with a considerable number of possibly differently typed, individually named function pointers, which you wish to initialize in a loop rather than manually writing code for each single one. For that, you would have an unnamed union with an array as one member and an unnamed struct containing the individual funcptrs as another member. Thus, you can initialize them all in an array manner, and still call them as if they belonged to the parent struct, which is very convenient and efficient too.

Share this post


Link to post
Share on other sites
ankhd    2304
I'm using it so I can pass data read from a file to my unit class when it creates the definition of this object,

some thing like this



class a

{

struct Initdata

{

char filenamemesh[128];

DWORD type;

};



a(){}

~a(){}

Initdata init;

};




//then do this

a baseobj;

a::Initdata savedata, loaddata;

do suff

Share this post


Link to post
Share on other sites
way2lazy2care    790
[quote name='SiS-Shadowman' timestamp='1311586372' post='4839915']
You are defining 2 types, [b]class a[/b] and [b]struct a::somemembers[/b]. However no instance of [b]a[/b] holds an instance of [b]a::somemembers[/b], since there is no "somemembers foo;" within the definition of [b]a[/b]. In my opinion it does make sense to define a type (for example a structure, enum or class) within another type if both belong together.
[/quote]

This is generally right, but I don't think it applies to all languages. That's kind of the fault of the question though as it is a language agnostic question looking for a language specific answer.

What language are you working in OP?

edit: :(

Share this post


Link to post
Share on other sites
SiCrane    11839
[quote name='way2lazy2care' timestamp='1311597176' post='4839959']
What language are you working in OP?
[/quote]
It's pretty clearly labelled C++ in the topic description area (otherwise known as the somewhat confusing area under the title).

Share this post


Link to post
Share on other sites
way2lazy2care    790
[quote name='SiCrane' timestamp='1311597604' post='4839962']
[quote name='way2lazy2care' timestamp='1311597176' post='4839959']
What language are you working in OP?
[/quote]
It's pretty clearly labelled C++ in the topic description area (otherwise known as the somewhat confusing area under the title).
[/quote]

ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh. So my optometrist was right when he said I needed glasses. I'VE BEEN LIVING A LIE :'(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this