C++ Passing Objects by Reference
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&);
开发者_如何学JAVAis 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.
I cannot reproduce the behavior you describe. As you can see here the copy-constructor is only called when you call FunctionOne
.
edit including the code directly here so that it is easier to read.
source:
#include <iostream>
class SimpleCat {
public:
SimpleCat() {
std::cout << "\tSimpleCat() called\n";
}
SimpleCat(SimpleCat&) {
std::cout << "\tSimpleCat(SimpleCat&) called\n";
}
};
SimpleCat FunctionOne( SimpleCat theCat ){
return theCat;
}
SimpleCat* FunctionTwo( SimpleCat* theCatPtr ){
return theCatPtr;
}
int main() {
SimpleCat cat;
std::cout << "-----\n";
std::cout << "FunctionOne{\n";
FunctionOne(cat);
std::cout << "}\n";
std::cout << "FunctionTwo{\n";
FunctionTwo(&cat);
std::cout << "}\n";
}
output:
SimpleCat() called
-----
FunctionOne{
SimpleCat(SimpleCat&) called
SimpleCat(SimpleCat&) called
}
FunctionTwo{
}
A constructor that takes a reference to it's own type is called a "copy constructor" (that will be in your book).
Whenever the compiler needs to make a copy of an object of certain type, if the class definition has a copy-constructor it will use that, otherwise it uses a default implementation that it provides. So the copy constructor allows you to take control of how your class is copied.
Most likely your copy constructor is being used when you call FunctionOne
, not FunctionTwo
because the parameter is being passed by value.
Your FunctionOne takes an object BY VALUE.
That means, that whenever the function is called, a copy is made of the datatype. This holds for ordinary datatypes like int, char, ..., but also for objects (in your case a SimpleCat object).
So when you have a function like void doIt(int a)
, then when you call doIt(3), the value 3 is copied by creating a new instance of an integer with the value of 3.
Translated to your FunctionOne
, calling FunctionOne(cat)
will make a copy of your cat object, resulting in the copy constructor being called.
精彩评论