C++ Nested Classes

Started by
7 comments, last by asdasd12345 17 years, 6 months ago
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?
http://www.geocities.com/asdasd12345/
Advertisement
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.
cool thanks buddy
http://www.geocities.com/asdasd12345/
I think you mean:

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

Is that what you mean?

Dave
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
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.
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.
http://www.geocities.com/asdasd12345/
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};
[size=2]
thanks dude
http://www.geocities.com/asdasd12345/

This topic is closed to new replies.

Advertisement