Copy constructor not being called [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Copy Constructor is not invoked
# include <iostream>
using namespace std;
class Abc
{
public:
int a;
Abc()
{
cout<<"def cstr\n";
a=10;
}
Abc(const Abc &source)
{
a=source.a;
cout<<"copy constructor is called"<<endl;
}
};
int main()
{
Abc kk = Abc();
cout<<kk.a<<endl;
return 0;
}
In the above program my output is :
def cstr
10
Here I expected that copy constructor would be called after the default contructor which is not happening.
Please tell me whats going on here. Is it because Abc() is creating a temp object ??
Please correct me if I am wrong.
thanks !!!

Your copy constructor is ok, try that
int main() {
Abc kk;
Abc kk1 = kk;
cout<<kk.a<<endl;
return 0;
}
Copy constructor is called once on construction from another existing object. Other times assignment operator is called. By saying Abc kk = Abc(); you are just calling default constructor.

Related

why is copy constructor called twice in this code? [duplicate]

This question already has answers here:
std::vector init with braces call copy constructor twice
(2 answers)
Closed 9 months ago.
When I execute the below code, a copy constructor of AAA is called twice between boo and foo.
I just wonder when each of them is called exactly.
Code:
#include <iostream>
#include <vector>
class AAA
{
public:
AAA(void)
{
std::cout<<"AAA ctor"<<std::endl;
}
AAA(const AAA& aRhs)
{
std::cout<<"AAA copy ctor"<<std::endl;
}
AAA(AAA&& aRhs) = default;
};
void foo(std::vector<AAA>&& aVec)
{
std::cout<<"----foo"<<std::endl;
}
void boo(const AAA& a)
{
std::cout<<"----boo"<<std::endl;
foo({a});
std::cout<<"----boo"<<std::endl;
}
int main(void)
{
AAA a;
boo(a);
return 0;
}
Output:
AAA ctor
----boo
AAA copy ctor
AAA copy ctor
----foo
----boo
The copy constructor is invoked twice here:
foo({a});
First to construct the elements of the initializer list, and second to copy the values from the initializer list to the std::vector.

Why a constructor is called twice when an object is instantiated

Per my understanding, I know that when an object is instantiated, a constructor is called once. But I can't understand why both constructors are called and only one object is instantiated
#include <iostream>
using namespace std;
#define print(me) cout << me << endl;
class A
{
public:
A() { print("default called"); }
A(int x) { print("paramterized called"); }
};
int main()
{
A a;
a = A(10);
return 0;
}
I got output:
default called
parameterized called
In these lines
A a;
a = A(10);
there are created two objects of the type A. The first one is created in the declaration using the default constructor
A a;
And the second one is a temporary object created in the expression A( 10 )
a = A(10);
that then is assigned using the copy assignment operator to the already existent object a.
Due to the copy elision you could avoid the use of the default constructor by writing initially
A a = A( 10 );
In fact due to the copy elision it is equivalent to
A a( 10 );
provided that the copy constructor is not declared as explicit.
You are also calling function while creating it with A a;
You can solve it by initializing value while creating it like this:
int main()
{
A a = A(10);
}
Or as a Fareanor said :
int main()
{
A a(10);
}

Move and copy constructor in C++ [duplicate]

This question already has answers here:
How does guaranteed copy elision work?
(2 answers)
Why isn't the copy-constructor called when returning LOCAL variable
(1 answer)
Closed 2 years ago.
#include <iostream>
class studant {
public:
studant () {
std::cout<<"studant"<<std::endl;
}
studant(const studant& a) {
std::cout<<"copy studant (&)"<<std::endl;
}
studant(studant&& a) {
std::cout<<"move studant (&)"<<std::endl;
}
studant maximum () {
studant c1;
return c1;
}
};
studant create () {
studant c1;
return c1;
}
int main()
{
studant c2;
studant c3=c2.maximum ();
studant c4=create ();
}
please see the above code why "studant c3=c2.maximum ()" and "studant c4=create ()" is not call the copy or move constructor. please explain me.
This is because the RVO (Return Value Optimization). studant c3=c2.maximum () calls maximum(), the compiler knows the c1 inside maximum() will be returned and then assigned to c3, and then c1 will discarded. The compiler is smart enough to create just one instance of studant to avoid the assignment. That means c1 inside maximum() is the same instance as c3.
This is same for 'c4' and c1 in function 'create()'.

Multiple constructor called for one object [duplicate]

This question already has an answer here:
Relationship between assignment operator and user-defined constructor
(1 answer)
Closed 5 years ago.
#include <iostream>
using namespace std;
class A
{
private :
int m_n;
static int copyconst;
static int copydest;
public :
A()
{
cout<<"Default constructor"<<endl;
}
A(int n):m_n(n)
{
cout<<"Param constructor"<<endl;
cout<<m_n<<endl;
}
A(const A&obj1)
{
++copyconst;
cout<<"Copy constructor"<<endl;
}
A& operator=(const A&obj)
{
cout<<"Assignment operator"<<endl;
}
~A()
{
++copydest;
}
};
int A::copyconst = 0;
int A::copydest = 0;
int main()
{
A a = 0;
a = 2;
}
Output -
Param constructor
0
Param constructor
2
Assignment operator
I am not able to understand why I am getting this output can any one help?
I am not able to understand why I am getting this output can any one help?
A a = 0;
is equivalent to:
A a(0);
That explains the first couple of lines of output.
a = 2;
is equivalent to:
a = A(2);
since there is no operator= function whose LHS is an A and the RHS is an int.
That explains the second couple of lines of output and the last line of output.
I believe you understand the first two prints.
Param constructor
0
This is because A a = 0; is a call to the constructor and not the assignment operator (as the object is being constructed)
When you do
a = 2;
then 2 is first converted to an object of type A because A can take int argument and you have not declared the constructor explicit. Hence you see this output
Param constructor
2
Next, the assignment operator is called to assign the value of A(2) to a and hence this output
Assignment operator

Why is the default constructor the only one being used? [duplicate]

This question already has answers here:
Copy constructor elision?
(2 answers)
Closed 8 years ago.
I am triying to see when each method is called in this example:
#include <iostream>
using namespace std;
class A {
public:
int x;
A(int x) : x(x) {cout<<"default ctor"<<endl;}
A(const A& a) : x(a.x) {cout<<"copy ctor"<<endl;}
A& operator =(const A& a) {cout<<"assignment op"<<endl;x=a.x;return *this;}
};
A f() { return A(5); }
int main() {
A a = f();
}
I expected the copy constructor to be called with the sentence return A(5) because as long as I know when an object is returned a temporary copy is created and returned. And also, in the sentence A a = f() I would expect the copy constructor to be called too because a is being initialized given another A object.
Why is default ctor being printed?
Two optimizations come into play here. Return Value Optimization (RVO)
And Copy Elision will merge f()'s return value directly into the destination variable via initialization. So this code:
A f() { return A(5); }
A a = f();
Optimizes to essentially:
A a(5);
Because c++ compilers are allowed to skip the operator= in cases of
A a = A();
A b = A(a);
and use constructors directly.
They are also allowed to perform Return Value Optimization for copy elision.
So in the end you have essentially A a(5);