Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is copy constructor called instead of conversion constructor?

Tags:

So basically this code:

class A { }; class B {     B (const B& b) {} public:     B (){}    B (const A& a) {}  };  int main() {    A a;    B b1(a);  //OK    B b2 = a; //Error } 

only generates an error for B b2 = a. And that error is

error: ‘B::B(const B&)’ is private

Why is it attempting to call the copy constructor in addition to the direct conversion constructor?

It's clear from the error message that a temporary B is created which is then used for copy-construction, but why? Where is this in the standard?

like image 765
Luchian Grigore Avatar asked Jun 27 '12 08:06

Luchian Grigore


People also ask

Why copy constructor is called?

A copy constructor is a member function that initializes an object using another object of the same class. The Copy constructor is called mainly when a new object is created from an existing object, as a copy of the existing object.

How is a copy constructor different from a constructor?

Constructor: It is a method which has the same name as the class which is used to create an instance of the class. Copy Constructor: Used to create an object by copying variables from another object of the same class. The main purpose is to create a new object from an existing one by copying variables.

Why there is no copy constructor in Java?

In C++ that statement makes a copy of the object's state. In Java it simply copies the reference. The object's state is not copied so implicitly calling the copy constructor makes no sense. And that's all there is to it really.

What is the difference between a copy constructor and an assignment operator?

The Copy constructor and the assignment operators are used to initializing one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space.


1 Answers

B b2 = a; 

This is known as Copy Initialization.

It does the following:

  1. Create an object of type B from a by using B (const A& a).
  2. Copy the created temporary object to b2 by using B (const B& b).
  3. Destroy the temporary object by using ~B().

The error you get is not at step 1 but rather at step 2.

Where is this in the standard?

C++03 8.5 Initializers
Para 14:

....
— If the destination type is a (possibly cv-qualified) class type:
...
...
— Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequences that can convert from the source type to the destination type or (when a conversion function is used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, the initialization is ill-formed. The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the destination type. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.

like image 147
Alok Save Avatar answered Oct 14 '22 00:10

Alok Save