You are going to implement an Has-a relationship between two classes. Which is the best way to model this relation: by reference, by pointer or by value?
By Value
In general this is the preferable way to implement an Has-a relationship, the main advantage is to have to complete ownership of the object. Let's have a look at this small chunk of code:
So far so good, every time we create a relationship between two objects we bond the behaviour of one object to the interface of the other one. I will use this simple example to show how the Owner class will change based upon the Component interface.
The constructor of Owner takes a const reference of a component object. The keyword const is important because we don't and we shouldn't change the object that we are going to copy and store on our class (the keyword explicit is to avoid undesired automatic conversion, have a look at Strongly Type post for more information).
The first drawback of storing the new component by value is performance, we are copying a value, so if Component object is big we spend a lot of time copying it. Please note that the object can be indefinitely big. For example a std::vector can be very-very-very big.
The author of Component is a good developer and he knows that copying a very-very-very big object is bad, so he decided to disallow the copy semantic (as above).
Now we have the first trouble, the Owner class will not compile! So, the sad moral of the story is: we cannot implement has-a relationship by value if the component object has not copy semantic.
This is not the worst case scenario, let's take as example this Component interface:
This time the copy semantic is allowed but the keywords const has been removed. Why? I don't know the people are different and the developers too. Removing the const keyword can give you a very small advantage in performance (do you really need it?) but it can spoil the day of another developer. How? This example explains it:
Changing an object that you take as an input parameter is like scratching the car of your friend. You shouldn't do that.
So: we can but we shouldn't implement an has-a relationship by value if the component object has non-const copy semantic.
Actually, we haven't finished yet with our analysis. There is another pitfall to avoid. It is a well known feature of C++, if you don't provide Constructor, Destructor, Copy-Constructor and Assignment Operator the compiler provide a version for you. You may think cool, less work. Actually there is huge drawback. The compiler is smart, but it is not a mind reader. Even the smartest compiler cannot reproduce the copy semantic that you think! You need to code it, otherwise the compiler will do the best and it can creating a bitwise copy. Bitwise copy is an exact copy of an existing object bit by bit. In order to show all the possible outcomes I have prepared a small program. Even though the program is completely meaningless the result output of it is exactly what we want:
The output is:
10 Built-in type
Foo Object with deep-copy semantic
0 10 20 30 40 50 60 70 80 90 Data stored by pointer
10 Built-in type
Foo Object with deep-copy semantic
0 100 200 300 400 500 600 700 800 900 Data stored by pointer
As you can see the Built-in type is copy correctly and also the string object. The array is doing something odd. The value of the array has been modified after the creation of Owner object. The reason is that the compiler makes a shallow copy of the pointer, it copies just the pointer and not all the data pointed by the pointer. So you will end up with two entities sharing the same data. Sometimes is what you want but very often is not.
So: we shouldn't implement an has-a relationship by value if the component object hasn't a deep copy semantic. If we really want to do that we need to carefully document our choice because it is not the behaviour that the client of our class will expect.
By Value
In general this is the preferable way to implement an Has-a relationship, the main advantage is to have to complete ownership of the object. Let's have a look at this small chunk of code:
- class Component
- {
- public:
- /* some stuff */
- private:
- /* some other stuff */
- };
- class Owner
- {
- public:
- Owner(const Component& component)
- :component_(component) {}
- private:
- Component component_;
- };
The constructor of Owner takes a const reference of a component object. The keyword const is important because we don't and we shouldn't change the object that we are going to copy and store on our class (the keyword explicit is to avoid undesired automatic conversion, have a look at Strongly Type post for more information).
The first drawback of storing the new component by value is performance, we are copying a value, so if Component object is big we spend a lot of time copying it. Please note that the object can be indefinitely big. For example a std::vector can be very-very-very big.
- class Component
- {
- private:
- /* some stuff */
- Component(const Component& other);
- Component& operator=(const Component& other);
- public:
- /* some other stuff */
- };
Now we have the first trouble, the Owner class will not compile! So, the sad moral of the story is: we cannot implement has-a relationship by value if the component object has not copy semantic.
This is not the worst case scenario, let's take as example this Component interface:
- class Component
- {
- public:
- /* some stuff */
- Component(Component& other);
- Component& operator=(Component& other);
- private:
- /* some other stuff */
- };
- Component comp;
- /* some calculation */
- /* comp is very important! My whole program
- is based on it! */
- Owner o(comp);
- /* Arggggg.... Owner can change it! */
So: we can but we shouldn't implement an has-a relationship by value if the component object has non-const copy semantic.
Actually, we haven't finished yet with our analysis. There is another pitfall to avoid. It is a well known feature of C++, if you don't provide Constructor, Destructor, Copy-Constructor and Assignment Operator the compiler provide a version for you. You may think cool, less work. Actually there is huge drawback. The compiler is smart, but it is not a mind reader. Even the smartest compiler cannot reproduce the copy semantic that you think! You need to code it, otherwise the compiler will do the best and it can creating a bitwise copy. Bitwise copy is an exact copy of an existing object bit by bit. In order to show all the possible outcomes I have prepared a small program. Even though the program is completely meaningless the result output of it is exactly what we want:
- #include <iostream>
- #include <string>
- using namespace std;
- class Component
- {
- public:
- /* some stuff */
- Component(int numberOfValue,string myString) :
- i_(new int[numberOfValue]),
- numberOfValue_(numberOfValue),
- myString_(myString)
- {
- changeValue(10);
- }
- ~Component() { delete[] i_; }
- int getNumberOfValue() const { return numberOfValue_; }
- string getMyString() const { return myString_; }
- void printArray() const
- {
- for (int j=0; j < numberOfValue_; j++)
- cout << i_[ j ] << " " ;
- cout << " Data stored by pointer" << endl;
- }
- void modify(int newNumberOfValue,string anotherString)
- {
- delete[] i_;
- i_ = new int[newNumberOfValue];
- numberOfValue_ = newNumberOfValue;
- myString_ = anotherString;
- changeValue(100);
- }
- private:
- void changeValue(int leverage)
- {
- for (int j=0; j < numberOfValue_; j++)
- i_[ j ] = leverage*j;
- }
- /* some other stuff */
- int* i_;
- int numberOfValue_;
- string myString_;
- };
- class Owner
- {
- public:
- Owner(Component& component)
- :component_(component)
- {
- component_.modify(component_.getNumberOfValue(), "Bar");
- }
- private:
- Component component_;
- };
- int main (int argc, char *argv[])
- {
- //numberOfValue equal to 10
- Component comp(10,"Foo");
- cout << comp.getNumberOfValue() << " Built-in type " << endl;
- cout << comp.getMyString() << " Object with deep-copy semantic" << endl;
- comp.printArray();
- //Make a copy of Component!
- //Using the compiler automatic
- //generated copy consturctor
- Owner o(comp);
- cout << comp.getNumberOfValue() << " Built-in type " << endl;
- cout << comp.getMyString() << " Object with deep-copy semantic" << endl;
- comp.printArray();
- return 0;
- }
10 Built-in type
Foo Object with deep-copy semantic
0 10 20 30 40 50 60 70 80 90 Data stored by pointer
10 Built-in type
Foo Object with deep-copy semantic
0 100 200 300 400 500 600 700 800 900 Data stored by pointer
As you can see the Built-in type is copy correctly and also the string object. The array is doing something odd. The value of the array has been modified after the creation of Owner object. The reason is that the compiler makes a shallow copy of the pointer, it copies just the pointer and not all the data pointed by the pointer. So you will end up with two entities sharing the same data. Sometimes is what you want but very often is not.
So: we shouldn't implement an has-a relationship by value if the component object hasn't a deep copy semantic. If we really want to do that we need to carefully document our choice because it is not the behaviour that the client of our class will expect.