0

I'm analysing a code and encountered the following structure. Whats the relation of Class1 and Class2? (the project has been developed on Qt framework).

Simpilified Code:

class Class1
{
    ...

private:
    class Class2
}

class Class1::Class2: public BaseClass
{
    ...
}

Thanks for any help.

Geezer
  • 5,600
  • 18
  • 31
Reza Sy
  • 7
  • 2

2 Answers2

4

It's called a nested class. It's basically a class declaration in another class declaration, quite similar to declaring a class inside a namespace.

If you make it private, only the outer class can access it. It's useful for organizing your implementation details without other classes or namespaces "seeing" it.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
2

Class2 is a private nested class inside Class1, One may wonder why this is used at all, there are many reasons amongst which I see the best to be the PImpl idiom.

PIMPL idiom, hides the implementation details, so you add all your implementation under Class::Class2, then the public interface to your Class is very thin, in other words you expose very less.

Samer Tufail
  • 1,835
  • 15
  • 25