Why is Constructor 2 invoked in CAT func(CAT someCat)? (After my knowledge) I am not passing any reference to CAT constructor within that function, so the normal CAT() constructor should be called instead.
Please help me understand this. If you could explain me step by step the entire process I would be very grateful to you.
You're passing a CAT by value, so the copy constructor (CAT::CAT(CAT &) or CAT::CAT(const CAT &). In your case you've only defined the former) is called. When passing references to a function no constructor needs to be called because no new object is constructed.
You're passing a CAT by value, so the copy constructor [...] is called.
Why is it called?
constructor 2 is called because passing object by reference on "func( frisky)"
Where is it passed by reference? Please have a look at my following comments and correct my judgment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// New function 'func' is declared. The returned value is of type CAT.
// Function takes as parameter a variable of type CAT: someCat
CAT func(CAT someCat)
{
cout << "CAT func(CAT): Returning new CAT..\n";
return someCat; // Function returns someCat which is of type CAT
}
[.....]
CAT Frisky; // Create 'Frisky' of type CAT
func(Frisky); // Call 'func' with 'Frisky' as parameter
Where is it passed by reference? Please have a look at my following comments and correct my judgment:
There are no passes by reference in the call to func().
Why is [the copy constructor] called?
Copy constructors are called to, unsurprisingly, construct copies. That's what it means to pass by value. You're passing to the callee a copy of an object in the caller. That copy has to be constructed somehow. and that's what the copy constructor is for.
That copy has to be constructed somehow. and that's what the copy constructor is for.
Can't be used the original constructor to do that? Why do I have to declare another constructor with reference as parameter?
I am confused because I don't know how the 'copy' of the object is actually done.
1. The object 'Frisky' is instantiated.
2. The function 'func()' is invoked by 'main()'
3. Another object identical with 'Frisky' is created with temporary scope for 'func()'
4. 'func()' definition is called
At part 3: why do I need another constructor with CAT value passed by reference to create a replica of 'Frisky'. Can't the original 'CAT()' be used to do that? If no, why not?
Because what the function gets is a COPY of the object passed in. Not a default-constructed object. You said it yourself:
Another object identical with 'Frisky'
What sense would that make, if it didn't matter what the value of the object you passed in was, because all the function ever saw was a default-constructed object?
Can't be used the original constructor to do that?
Two questions:
1. Suppose you have this class:
1 2 3 4 5 6 7
class A{
int a;
public:
A(){
this->a = /*???*/;
}
};
If A::A() was to be used as a copy constructor, how would you initialize A::a so that it has the same value as a different instance of A?
2. It's possible for a class to not have a default constructor (the constructor that takes no parameters). For example:
1 2 3 4 5 6 7
class A{
int a;
public:
A(std::string *foo){
this->a = /*???*/;
}
};
What should the compiler pass to this constructor?
why do I need another constructor with CAT value passed by reference to create a replica of 'Frisky'.
You need someplace to get the data from which the copy will be made. In your case CAT has no data, so you could have simply defined no copy constructor (the compiler would have defined one for you), but what if the class did have data?
1 2 3 4 5 6 7
class A{
int a;
public:
A(const A &other){
this->a = other.a;
}
};
In some ways this is like the Miranda law. My paraphrasing:
You have the right to remain silent. Anything you say may used against you. You have the right to an attorney... if you cannot afford one , one will be provided for you
To continue my analogy,
- "silent": not needing to explicitly define constructor, destructor, copy constructor, assignment operator, move constructor, etc.
- anything you "say" may be used against you: if you do make one or more of the above, they will be prioritized
- if you cannot "afford": compiler will provide them for you automagically
Perhaps some more information about the objects you're creating and destroying will be helpful: