It is not treated like a value type, in fact it is.
While in Java object variables store references to objects, in C++ there is an important difference between an object and its reference. Assignment is by default really by value.
If you want a variable to be just a reference, you use either a reference or a pointer type, depending what you want to with it. These types are declared T*
and T&
.
To illustrate this a little more:
In Java, when you say MyClass obj
, an object is created, but a reference/pointer is stored in the variable obj
.
In C++, MyClass obj
creates the object and will stored it in obj
. If you want to work with references/pointers, you need to declare variables explicity as MyClass* objPointer
or MyClass& objReference
.