22.7 Instance members and reference members

We often have members of classes which are themselves classes. Thus we might have something like this.

class MyClass 
    ClassA aobject; 
    ClassB *pbobject; 

In this kind of situation, we say that aobject is an embedded or instance ClassA member, while pbobject is a pointer-based or reference ClassB member. We often prefer to use pointer-based members because then the methods of these members can be called polymorphically. When we talk about Serialization, Chapter 30, we'll see that there are some important issues relating to the difference between embedded and pointer-based members.

Generally you let primitive variables be instance members, and you let your object members be references. This is, in fact, the Java style of doing things. You should generally have in the back of your mind that you might need to port your C++ to Java (or C#) one of these days, or vice-versa, so, all things being equal, the more you can make your C++ code like Java and your Java code like C++ code the better.

A big win with having pointer-based reference members is that then polymorphism will work for these members ? see Section 22.11: Polymorphism.

    Part I: Software Engineering and Computer Games
    Part II: Software Engineering and Computer Games Reference