Alright so I'm not saying this is exactly why objects are reference types or passed by reference but I can give you an example of why this is a very good idea in the long run.
If I'm not mistaken, when you inherit a class in C++, all the methods and properties of that class are physically copied into the child class. It would be like writing the contents of that class again inside the child class.
So this means that the total size of the data in your child class is a combination of the stuff in the parent class and the derived class.
EG:
#include
class Top
{
int arrTop[20] = {1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1};
};
class Middle : Top
{
int arrMiddle[20] = {1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1};
};
class Bottom : Middle
{
int arrBottom[20] = {1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1};
};
int main()
{
using namespace std;
int arr[20];
cout << "Size of array of 20 ints: " << sizeof(arr) << endl;
Top top;
Middle middle;
Bottom bottom;
cout << "Size of Top Class: " << sizeof(top) << endl;
cout << "Size of middle Class: " << sizeof(middle) << endl;
cout << "Size of bottom Class: " << sizeof(bottom) << endl;
}
Which would show you:
Size of array of 20 ints: 80
Size of Top Class: 80
Size of middle Class: 160
Size of bottom Class: 240
This means that if you have a large hierarchy of several classes, the total size of the object, as declared here would be the combination of all of those classes. Obviously, these objects would be considerably large in a lot of cases.
The solution, I believe, is to create it on the heap and use pointers. This means that the size of objects of classes with several parents would be manageable, in a sense.
This is why using references would be a more preferable method for doing this.