Reputation: 37
I'm studying for an OOP test in C++, and I'm trying to understand something about the idea of constructors.
In the code below:
Class A:
#pragma once
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "1"; }
A(const A& a) { cout << "2"; }
~A(){ cout << "3"; }
A anyFunc(A& a1, A* a2, A a3) {
cout << "4";
return *a2;
}
};
Main:
#include"A.h"
void main() {
A a;
a.anyFunc(a, &a, a);
}
This code prints "1242333"
.
I understand the first "1...."
because we create an object a
of type A
with the default constructor.
Later on, we send to the member function of class A
the object we created first by reference, then as a pointer, and last by value.
I don't understand what the compiler does after that. Why would it go and use the copy constructor and print "2"
, then print "4"
, and then again go to the copy constructor to print "2"
? Basically, creating a2
and a3
. I understand the last "....333"
that are printed, since if we created the local static objects a1
,a2
,a3
then the compiler calls the destructor when those variables "die" in the end.
Trying to understand how constructors work in C++.
Upvotes: 1
Views: 157
Reputation: 595497
1
is printed when the variable a
in main()
is default-constructed (object #1).
2
is printed when the a3
parameter of anyFunc()
is copy-constructed (object #2) from a
. Since a3
is passed in by value, it is a copy of whichever A
object is given to it. The a1
and a2
parameters do not create new objects, they simply refer to existing objects (in this case, to the same object - a
in main()
).
4
is printed when anyFunc()
begins running. No new A
object is created here.
2
is printed again when the return value of anyFunc()
is copy-constructed (object #3) from *a2
. Since anyFunc()
returns an A
object by value, it returns a temporary copy of whichever A
object is being return
'ed. The caller of anyFunc()
decides what it wants to do with that returned copy - save it to a local variable, pass it to another function, or just ignore it (as your example is doing).
3
is printed when the a3
parameter of anyFunc()
is destructed when anyFunc()
exits.
3
is printed when the temporary A
object returned by anyFunc()
is destructed.
3
is printed when the a
variable in main()
is destructed when main()
exits.
Upvotes: 2
Reputation: 310930
For starters according to the C++ Standard the function main
without parameters shall be declared like
int main()
As for your question then in this statement
A a;
there is called the default constructor
"1 "
then in this call
a.anyFunc(a, &a, a);
for the third argument to create the object that corresponds to the third parameter a3
there is used the copy constructor
"12 ".
The function gets the control
"124 ".
The function returns an object of the type A
using the expression *a
A anyFunc(A& a1, A* a2, A a3) {
cout << "4";
return *a2;
}
So again the copy constructor is called to create the returned object
"1242 ".
Then the destructor is called for the object that corresponds to the parameter a3
"12423 ".
Then destroyed the temporary returned object
"124233 ".
And at last there is destroyed the object defined in main in this declaration
A a;
As a result you have
"1242333".
That is there were created three objects. The first one - in main. The second one that corresponds to the parameter a3
. And at last the returned temporary object. For each of these objects there was called the destructor.
Pay attention to that if you would call the function like
a.anyFunc( a, &a, A() );
then the ouput would be
1142333
That is instead of the second digit 2
there will be the digit 1
due to the copy elision.
Upvotes: 3