Question:
C++ why is the copy constructor no called?
Martin
2013-06-10 16:18:27 UTC
See this code,

class A
{
A(const A& other)
{
cout << "copy constructor";
}
};

int main()
{
A a;
A b(a);
}

I don't get any output. Why is it not called?
Four answers:
cristi_192
2013-06-10 16:34:59 UTC
1. your copy constructor is private so main() cannot call it.

2. you should declare A() constructor, I'm not sure what the standard says, but I think that the compiler won't generate a default constructor for you since you declared a copy constructor.
2013-06-10 16:30:43 UTC
The assignment operator is used to copy the values from one object to another already existing object. The key words here are “already existing”. Consider the following example:



Cents cMark(5); // calls Cents constructor

Cents cNancy; // calls Cents default constructor

cNancy = cMark; // calls Cents assignment operator



In this case, cNancy has already been created by the time the assignment is executed.



Consequently, the Cents assignment operator is called. The assignment operator must be overloaded as a member function.



What happens if the object being copied into does not already exist? This is where the copy constructor is called.



Consider the following example:



Cents cMark(5); // calls Cents constructor

Cents cNancy = cMark; // calls Cents copy constructor!

Because the second statement uses an equals symbol in it, you might expect that it calls the assignment operator. However, it doesn’t! It actually calls a special type of constructor called a copy constructor. A copy constructor is a special constructor that initializes a new object from an existing object.



The purpose of the copy constructor and the assignment operator are almost equivalent — both copy one object to another. However, the assignment operator copies to existing objects, and the copy constructor copies to newly created objects.



The difference between the copy constructor and the assignment operator causes a lot of confusion for new programmers, but it’s really not all that difficult. Summarizing:



a - If a new object has to be created before the copying can occur, the copy constructor is used.

b - If a new object does not have to be created before the copying can occur, the assignment operator is used.



I apologize for the length, but I hope I helped clear things up.
wowwee
2013-06-10 19:46:49 UTC
You didn't call the copy constructor.

A a(b) is how you would call it (upon construction, not sometime later).
leatherman
2016-12-21 10:32:03 UTC
classification some thing{ public: some thing; //default +or some thing( const some thing& cloneMe ){ this->abc = cloneMe.abc }// some thing( const int& setThisAttribute ){ this->abc=setThisAttribute; } //records int abc; }; //so... some thing x; //makes use of the default +or to create x some thing y(x); //makes use of your replica +or to create a y that's a a twin of x some thing z(777); //makes use of the parametrized +or to set some fee up in z


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...