I am trying to learn C++, and I am working through “Sams Teach Yourself C++ in 21 Days”.
I have been progressing quite well so far, and even got through the chapter on pointers without difficulty. However, a listing on “Passing Objects by Reference” has left me quite confused.
There is a class with two constructors:
class SimpleCat
{
public :
SimpleCat();
SimpleCat(SimpleCat&);
...
};
two functions with the prototype:
SimpleCat FunctionOne( SimpleCat theCat );
SimpleCat* FunctionTwo( SimpleCat *theCat );
/ What is confusing me is that when calling the second function, the second constructor SimpleCat(SimpleCat&); is called. Could someone please explain? Any further searching has left me equally confused. /
EDIT: I have made a mistake in my post here, the copy constructor (as I now know what it is, thank-you so much ) is called with the first function. I am sorry for the confusion. I know understand the link now and you have all helped tremendously.
SimpleCat(SimpleCat&)is a copy constructor.SimpleCat FunctionOne(SimpleCat theCat)uses pass by value semantics. This requires that the class instance be copied. Hence the call to the copy constructor.