-
Nested class
Hi everyone,
Anyone can tell in which situations nested class becomes the best solution.
Of course we learnt it from textbook, but so far in real practices, I never used, infact avoid it. At some point, it makes the code complex and difficult to debug.
Anyone with me? ;)
-
I haven't heard of nested classes being available in C++, and my understanding until now is that they aren't; but even if they are, it's a very weird thing to do in that language and something that you probably generally just want to avoid.
-
The first example that comes to my mind is iterators in the STL.
To my knowledge, the containers in the STL implement their own versions of all iterators privately (probably as nested classes), then typedef them to just be "iterator" publicly.
It's about encapsulation. When you want an object that is similar between classes, but specific to a class.
For example, we are both people. For all intensive purposes, we are probably pretty much the same.
But let's pretend that we are citizens of different countries, and that because of this, we greet people differently.
Consider this sample code (or see it run here):
Code:
#include <iostream>
class CountryA
{
class CountryA_Citizen; // forward declaration
public:
typedef CountryA_Citizen citizen;
static citizen new_citizen() { return citizen(); }
private:
class CountryA_Citizen
{
public:
CountryA_Citizen() {}
std::string greeting() { return "Hey! How have you been?"; }
};
};
class CountryB
{
class CountryB_Citizen; // forward declaration
public:
typedef CountryB_Citizen citizen;
static citizen new_citizen() { return citizen(); }
private:
class CountryB_Citizen
{
public:
CountryB_Citizen() {}
std::string greeting() { return "Oi, oi! What's the story, mate?"; }
};
};
int main(void)
{
CountryA::citizen a = CountryA::new_citizen();
CountryB::citizen b = CountryB::new_citizen();
std::cout << "In CountryA they say: '" << a.greeting() << "'" << std::endl;
std::cout << "In CountryB they say: '" << b.greeting() << "'" << std::endl;
}
So, as a user, all I have to know is that any given country will have a citizen, and that all citizens have a greeting.
But from the developer's standpoint (ours), the internals of that citizen can work any way we please, as long as they still match the general interface. We can abstract away all of the side-effects that the user shouldn't be concerned with, and just give them the functionality they are looking for.
And generally nested classes aren't too confusing. They can be debugged pretty much as easily as any other part of the class.
-
nested classes are absolutely allowed in C++. in order to access them from outside, you have to use the fully qualified name.
Code:
class x
{
public:
class y
{
};
y bar;
};
x myX;
x::y& bar = myX.bar;
perfectly acceptable code.
when and where to use it is another story altogether though.
-
A list/tree class might consist of the list/tree class with a nested node class that contains the data and pointers. The main list/tree class would then have a pointer to the root node or first/last nodes and the member functions to operate on the internal nodes (insertion/searching/deletion/etc.).