22.7 Instance members and reference members


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  {  protected:      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.



Software Engineering and Computer Games
Software Engineering and Computer Games
ISBN: B00406LVDU
EAN: N/A
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net