Lets say I have the following:
class MyClass
{
// ...
}
void doSomething (MyClass instance)
{
// Is instance passed by reference or by value (copied)?
}
void main ()
{
MyClass instance = MyClass();
doSomething(instance);
}
In doSomething() is instance passed by reference? Or is the class duplicated in memory? Or something else?
This is passed by value
This is passed by reference
This is passed by const reference
Also
MyClassdoesn’t need to by constructed by assignment.So:
is effectively the same as:
EDIT:
This is passed by const reference to a const function, a const function can be called on a const object as it guarantees not to modify the object state.
Const correctness is considered good practice in C++ unlike many less strict languages.
see me:
http://en.wikipedia.org/wiki/Const-correctness
http://www.gotw.ca/gotw/006.htm