Sign in to follow this  

Define a structure with in a class good or bad idea

This topic is 2335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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
[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
[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
[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

This topic is 2335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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