K
krzysztof.konopko
Hello!
I want to design a class hierarchy with one base abstract class, let's
say CBase. I have a predicate that every object in the class hierarchy
must have a parent object of type from this class hierarchy but only
objects of some of these types can be a parent.
Example:
class CFruit : public CBase { };
class CBranch : public CBase { };
Class CBranch can be a parent for another object (CBranch or CFruit)
but CFruit cannot be a parent for any object.
It is tempting to put a CBase pointer in CBase class to fulfill
predicate that every object must have a parent but I want to force
proper object type for parent pointer by design structure and not the
runtime check (it may happend by accident the CFruit was made a parent
by upcast).
It is possible to put forward CBranch class declaration (which is a
base for other parent-type classes) in CBase class header file and put
a pointer to it in a base class but I do not want to show off any
derived classes in a base class header file.
It is possible to use an object factory which may try to guard proper
object creation for parent object but this don't convince me. Parent-
type objects may have different constructors than child-only-type
objects but this is hard to maintain.
Is there any design pattern for that problem?
I want to design a class hierarchy with one base abstract class, let's
say CBase. I have a predicate that every object in the class hierarchy
must have a parent object of type from this class hierarchy but only
objects of some of these types can be a parent.
Example:
class CFruit : public CBase { };
class CBranch : public CBase { };
Class CBranch can be a parent for another object (CBranch or CFruit)
but CFruit cannot be a parent for any object.
It is tempting to put a CBase pointer in CBase class to fulfill
predicate that every object must have a parent but I want to force
proper object type for parent pointer by design structure and not the
runtime check (it may happend by accident the CFruit was made a parent
by upcast).
It is possible to put forward CBranch class declaration (which is a
base for other parent-type classes) in CBase class header file and put
a pointer to it in a base class but I do not want to show off any
derived classes in a base class header file.
It is possible to use an object factory which may try to guard proper
object creation for parent object but this don't convince me. Parent-
type objects may have different constructors than child-only-type
objects but this is hard to maintain.
Is there any design pattern for that problem?