Sign in to follow this  
tom_mai78101

Nested Classes in C++: I need to know the end of each class' scopes

Recommended Posts

My purpose is to learn how to use nested class in a more efficient way, so that I can then move on to nested namespaces and complex object-oriented paradigms in C++.

Here, I have an example, created within the Visual Studio 2008 C++:

[source lang="cpp"]

[font="Courier New"]#include <cstdlib>[/font]

[font="Courier New"]class outside
{
private:
int Bound;
class within
{
private:
int Within;
public:
void setVariable();
};
public:
outside();
class nested
{
private:
int Nest;
public:
void setVariable();
};
};[/font]

[font="Courier New"]void outside::nested::setVariable()
{
outside::nested::Nest = 5;
}[/font]

[font="Courier New"]void outside::within::setVariable()
{
outside::within::Within = 20;
}[/font]

[font="Courier New"]outside::outside()
{
outside::within::setVariable(); //<-------ERROR
}[/font]

[font="Courier New"]int main()
{
outside Test;
outside::nested Test2;
Test2.setVariable();
return 0;
}[/font]

[/source]

My compiler gave me the error code C2662: Cannot convert "this" from "outside" to "outside::within &".

Looking up the [url="http://msdn.microsoft.com/en-us/library/2s2d2tez(v=VS.90).aspx"]C2662 error[/url] didn't really give me much details, so can I ask someone to help explain my problem? Thanks.

Share this post


Link to post
Share on other sites
You appear to be calling setVariable() in a static manner. You need an instance of "within" first, as you want to call a member function.

[quote]
My purpose is to learn how to use nested class in a more efficient way...
[/quote]
I don't understand this statement - more efficient than what exactly?

Share this post


Link to post
Share on other sites
Thank you, rip-off.

As for the quote, I'm learning about nested classes, nested namespaces, and such complexities. Learning these things deeply is going to help me develop a more thorough logical thinking.Thinking in a more efficient way, not about optimizings and performances.

Share this post


Link to post
Share on other sites
The main time I use nested classes is for delegating behaviour. If some of my classes want to listen for events then they need to inherit from IEventListener, alot of the time I just don't want to do that. Instead I make a private, nested class and create an instance of it. I can then add that to my event manager and have it control the "parent" object.

class A
{
private:
class Listener;
Listener *m_Listener;
};

Everything else can be completely hidden in the implementation of A. It also allows me to have objects register themselves when they are stored as a shared_ptr.

void RegisterListener(shared_ptr<Listener> a);

class A: public Listener {};

shared_ptr<A> a(new A);

Since Register takes a shared_ptr, A cannot register itself (using its this pointer). If we instead have A own a "Listener" type object, it can register that listener. The listener can either directly alter A, or could just pass on events to a method of A. Since these two classes would them be useless without each other, its a good idea to make one nested.

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