This is the scenario.
I have a Layer class which I want to create a subclass of, let’s call this ParentLayer class. The ParentLayer class will have yet another Layer subclass, called the ChildLayer class. Which is the best way to do it?
Declare ChildLayer inside ParentLayer as a private class.
// LayerSubclass.h
#include "Layer.h"
class ParentLayer : public Layer {
private:
class ChildLayer : public Layer {
private:
// ChildLayer members
public:
// Stuff...
};
ChildLayer _childLayer;
public:
// Stuff...
};
Declare ChildLayer as a separate class within the same header file.
// LayerSubclass.h
#include "Layer.h"
class ChildLayer : public Layer {
private:
// ChildLayer members
public:
// Stuff...
};
class ParentLayer : public Layer {
private:
ChildLayer _thisChild;
public:
// Stuff...
};
I’ve been using the first method but then I thought of the second method (which is cleaner and much more readable). Thoughts?
I might prefer to use a nested class:
friendaccess to private members of the containing class (which for example may be true of iterator classes, which access members of the corresponding container class)