• Advertisement
Sign in to follow this  

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

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

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 C2662 error 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
Advertisement
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.


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
Sign in to follow this  

  • Advertisement