“this [using an
AbstractTreeItem*
argument] is not possible as AbstractTreeItem is an abstract class”
is incorrect.
I.e. there is no technical problem.
There does appear to be a design level problem, but that's a different kettle of fish.
Domanda
I've got an abstract class that represents an item of a tree. As this class already inherits from another (not abstract) class I want to use the same structure in the constructor as the base class:
class TreeItem //already given class
{
public:
TreeItem(TreeItem *parent = 0);
...
};
class AbstractTreeItem : public TreeItem //class with some abstract methods
{
public:
AbstractTreeItem(TreeItem *parent = 0);
...
};
But I want to assure that all classes which will inherit of my abstract class only have children of AbstractTreeItem, too. Therefore I would like to use a constructor like the following:
class AbstractTreeItem : public TreeItem
{
public:
AbstractTreeItem(AbstractTreeItem *parent = 0);
...
};
And this is not possible as AbstractTreeItem is an abstract class. So is there a possiblity to achieve this in another way?
Soluzione
“this [using an
AbstractTreeItem*
argument] is not possible as AbstractTreeItem is an abstract class”
is incorrect.
I.e. there is no technical problem.
There does appear to be a design level problem, but that's a different kettle of fish.
Altri suggerimenti
So, you probably will have an non-abstract class:
class NonAbstractTreeItem : public AbstractTreeItem
{
public:
NonAbstractTreeItem(AbstractTreeItem *parent = 0): AbstractTreeItem(parent) {
...
}
...
};
that will be calling the AbstractTreeItem constructor.
If you want to underline that the constructor is only intended to be called from a child class, you can make it protected:
class AbstractTreeItem : public TreeItem
{
protected:
AbstractTreeItem(AbstractTreeItem *parent = 0);
...
};