By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,470 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,470 IT Pros & Developers. It's quick & easy.

Designing a class hierarchy with parents and children

P: n/a
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?

May 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 12, 10:51 am, krzysztof.kono...@gmail.com wrote:
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?

May 12 '07 #2

P: n/a
On 12 Maj, 22:10, dave_mikes...@fastmail.fm wrote:
On May 12, 10:51 am, krzysztof.kono...@gmail.com wrote:
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?
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)).

May 12 '07 #3

P: n/a
On May 12, 11:26 am, krzysztof.kono...@gmail.com wrote:
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/arti...Knowledge.html

May 12 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.