Sign in to follow this  

C++ Nested Classes

This topic is 4089 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

I am pretty new to programming in C++. I have a problem with nested classes. class Outer { public: class Inner { void Function(int ); }; }; void Outer::Inner::Function(int N) { N=0; } int main() { Outer O; O.Inner I;//this is wrong right here } my question is, how do I make a new object within the Outer object? Does that make sense? Am I allowed to do that?

Share this post


Link to post
Share on other sites
Use the scope resolution operator ::

int main( )
{
Outer::Inner I;
}


Think about it like this: In this case the outer class is working kind of like a namespace. You don't need an outer object to make an inner object, but you nned to specify where the Inner class is at.

Hope that made sense.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
I think you mean:

Outer::Inner myInner = new Outer::Inner();

Is that what you mean?

Dave

This is C++, so he probably didn't want that new in there at all. Otherwise that would work fine.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by asdasd12345
I am pretty new to programming in C++. I have a problem with nested classes.

class Outer
{
public:
class Inner
{
void Function(int );
};
};

void Outer::Inner::Function(int N)
{
N=0;
}

int main()
{
Outer O;
O.Inner I;//this is wrong right here
}

my question is, how do I make a new object within the Outer object? Does that make sense? Am I allowed to do that?


My psychic brainwaves tell me that you are coming from a Java background. This is based on the fact that you're "new to C++" and want to mess around with nested classes, and are talking about "making new objects".




Warning! In C++, nesting of classes is *only* a matter of scope. Although the Outer class contains the Inner class, no Outer *object* actually contains an Inner *object* unless you explicitly specify a data member of that type.

If that's what you want, then yes, just use the scope resolution operator (::) upon the class, instead of member selection (.) upon an instance. That will construct an independant Inner instance (technically, an Outer::Inner instance).

If Outer objects are supposed to contain Inner objects, then (a) as noted, you need to make a member of that type and (b) instantiating the Outer would then automatically instantiate the contained Inner.

You have options at that point:

1) Pass extra parameters to the Outer constructor, which it will forward to the Inner constructor for its construction. (Or in the case that Inner has a zero-arg constructor that you want to use, just do nothing special). Example:


class Outer {
class Inner {
int bar;

public:
Inner (int bar): bar(bar) {}
};
int foo;
Inner i;

public:
Outer(int foo, int bar): foo(foo), i(bar) {}
};


2) If the Inner member is public, assign to it from outside. But be aware that it's already constructed at that point, so now we're overwriting the initial value.

3) If you *really* are bent on mimicking Java in C++, use an Inner* member, and then allocate it with new:


struct Outer {
struct Inner {};
Inner* i;
};

Outer o;
o.i = new Inner();


However, you are now responsible for the memory management. In C++, doing things this way is begging for a world of hurt and is totally unnecessary.

Share this post


Link to post
Share on other sites
i think i kind of understand, but i have a question. lets pretend this code below would work

class Drawer
{
public:
class Utensil
{
int sharpness;
int length;
};

int AmountofUtensils;
}

int main
{
Drawer KitchenDrawer;
Utensil Spoon;
Utensil Knife;
Utensil Fork;
cout<<KitchenDrawer.Knife.sharpness;
}

Do you get what I am trying to do? Im trying to make a big class, and to put as many objects in the big class container. I'm going to have a big class called Sprites, and then different states the Sprite could have like WalkingLeft, WalkingRight, each which contain the individual frames to animate it? I guess I am struggling to see how the code you showed me could work with that idea.

Share this post


Link to post
Share on other sites
Quote:
Original post by asdasd12345
i think i kind of understand, but i have a question. lets pretend this code below would work

class Drawer
{
public:
class Utensil
{
int sharpness;
int length;
};

int AmountofUtensils;
}

int main
{
Drawer KitchenDrawer;
Utensil Spoon;
Utensil Knife;
Utensil Fork;
cout<<KitchenDrawer.Knife.sharpness;
}

Do you get what I am trying to do? Im trying to make a big class, and to put as many objects in the big class container. I'm going to have a big class called Sprites, and then different states the Sprite could have like WalkingLeft, WalkingRight, each which contain the individual frames to animate it? I guess I am struggling to see how the code you showed me could work with that idea.


Nesting classes is not what you want. Look into the container classes in the STL, particularly std::vector. For example:


sruct Utensil {
int sharpness, length;
};

class Drawer {
std::vector<Utensil> utensils;
public:
std::vector<Utensil>::size_type AmountOfUtensils() { return utensils.size(); }
// more methods to add and take specific utensils
};

Share this post


Link to post
Share on other sites

This topic is 4089 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