What if something is wrong with a child class containing a parent class object in C ++?

In C ++ specifically, but also generally as a design principle for OO, is there anything wrong with doing the following? Is this done in practice? If this shows a clear design flaw, what is a good alternative? Are there any advantages?

class Property {}; class CompositeProperty : public Property { ... private: std::vector<Property> m_properties; }; 

In particular, can a derived class contain objects of a base class?

Since I have a bit of background, I saw that it was used to model / mirror the XML structure, but believed that the design went a little in the face of the is-a-is-inheritance and has-a-is-composition relationships that are usually sought for.

+4
source share
5 answers

There is no shortage of design - in fact, this design is just one step away from the well-known and very useful composite template . However, there is a significant drawback in the implementation.

Your CompositeProperty combines Property instances, not aggregates pointers. This kills the ability to use CompositeProperty elements polymorphically. To solve this problem, you need to replace the instance vector with a vector of pointers (preferably smart pointers).

The classic place for a compound template is to represent expression trees: you start with an abstract base, and then add representations for constants, variables, function calls, unary expressions, binary expressions, conditional expressions, etc. Expressions, such as constants and variables, do not refer to other expressions, while expressions, such as unary expressions, binary expressions and function calls. This makes a recursive object graph, allowing you to represent expressions of arbitrary complexity.

+6
source

In particular, can a child class contain objects of the parent class?

Shorter than YES

+2
source

Pay attention to the Composite template .

Composite can be used when clients must ignore the difference between composition of objects and individual objects. If programmers find that they use several objects in the same way, and often have almost identical code to process each of them, then composition is a good choice; in this situation it is less difficult to consider primitives and composites as homogeneous.

+2
source

There is nothing wrong.

Take this example (this is C #, but should be simple enough), where you define two subclasses, each of which contains an instance of the inherited:

 public class Person { public string Name; } public class MalePerson : Person { public Person BestFriend; } public class FemalePerson : Person { public Person BestFriend; } 

In my experience, including an instance of a superclass in subclasses, it is most often used to model a hierarchy between heterogeneous objects or to refer to an object with as few assumptions as possible.

+2
source

There is nothing wrong with that, but you might want to use pointers for the base / parent class. This allows the polymorphic behavior of your objects. If you add instances of derived classes to your vector as it is, you will suffer from overlapping objects.

+2
source

All Articles