Designing a class hierarchy with parents and children

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?
 
D

dave_mikesell

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 { };

What exactly are you trying to model? If you can't use your concrete
classes through an ABC pointer without casting, inheritance may be the
wrong approach.

What does your ABC interface look like?
 
K

krzysztof.konopko

What exactly are you trying to model? If you can't use your concrete
classes through an ABC pointer without casting, inheritance may be the
wrong approach.

What does your ABC interface look like?

I am building some hierarchy tree. I have a sort of objects: main
containers (scopes), internal containers (scopes), regular objects
(leaves).
Main contaitainers (scopes) can contain main containers and internal
containers and all regular objects.
Internal containers can contain internal containers and some subset of
regular objects.

I need to build a tree (analyze the hierarchy). I do not know at which
point of the hierarchy I start (main or intelnal container or regular
object).
I receive some starting point and its type in some representation
which I can dispatch with abstract factory.
I want to say to this starting point (object) : "build hierarchy!" and
it should know what it contains (if it can) and it can call its
children for building hierarchy (all objects can gather proper
information for building the hierarchy through some interface which is
already available in the design).

At the end I want to traverse all regular objects (leaves of the tree)
and make some action with them ( they need to ask their parents for
some informations which will be available only after building a tree
(at least the part ending with the current leaf)).
 
D

dave_mikesell

I am building some hierarchy tree. I have a sort of objects: main
containers (scopes), internal containers (scopes), regular objects
(leaves).
Main contaitainers (scopes) can contain main containers and internal
containers and all regular objects.
Internal containers can contain internal containers and some subset of
regular objects.

I think the Composite Design Pattern may be helpful. Here is a link
to a C++ report article that uses Composite and Visitor for a tree.
Not sure if it's the exact solution you need for your problem, but
maybe it will set you on the right track.

http://members.home.nl/r.f.pels/articles/misc/C++PatternsAndTreesSeparatingKnowledge.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top