Why is that when declaring a copy constructor, the object being passed as argument to that constructor is (1) passed by reference using "&", and (2) the instantiated object is typecast as "const"?
Thanks keskiverto. Thanks for your patience as I am an newbie. Two follow up questions if you don't mind please:
(1) In "const MyClass &obj", is obj being passed by value or by reference (i.e. its address)?
(2) Aren't we typecasting obj to be of type MyClass, and if so, why use const on top?
1) by reference is not by address (although internally it may be done using pointers). Yes, obj is passed by reference (not by address). To pass by address it would be MyClass* obj
2) const means that the contents of obj can't be changed in the function. Hence the passed-in value can be const or could be an r-value.
During that function call the parameters are created like this:
1 2 3 4
MyClass one = obj; // by value parameter, copy construction
MyClass& two = obj; // by reference. ERROR: obj is const
const MyClass& three = obj; // by reference. ok
const MyClass* ptr = &obj; // by value. ok