为什么是复制构造函数而不是转换构造函数? [英] Why is copy constructor called instead of conversion constructor?

查看:167
本文介绍了为什么是复制构造函数而不是转换构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上这个代码:

  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; //错误
}

只会产生 B b2的错误= a 。这个错误是


错误:'B :: B(const B&)'是私人的


为什么除了直接转换构造函数之外,还试图调用复制构造函数?



错误消息,创建一个临时 B ,然后用于副本构建,但为什么?

  B b2 = a;  


这被称为 复制初始化



它符合以下条件:


  1. 创建<$ c通过使用 B(const A& a),从 a / li>
  2. 使用 B(const B& b) b2 c>。

使用〜B()销毁临时对象。

您得到的错误不是在步骤1,而是在步骤2.


>
Para 14:


....

- 如果目标类型是(可能是cv限定的)类类型:

...

...

- 否则(即,对于剩余副本 - 初始化情况),如13.3.1.4所述枚举可以从源类型转换到目标类型或(当使用转换函数时)到其派生类的用户定义转换序列,并选择最佳的转换序列通过重载分辨率(13.3)。如果转换不能完成或不明确,则初始化不成立。选择的函数调用初始化表达式作为其参数;如果函数是构造函数,则调用将初始化目标类型的临时。调用的结果(其是构造器情况的临时)然后用于根据上述规则来直接初始化作为复制初始化的目的地的对象。在某些情况下,允许实现通过将中间结果直接构造到正被初始化的对象中来消除这种直接初始化中固有的复制;请参阅12.2,12.8。



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?

解决方案

B b2 = a;

This is known as Copy Initialization.

It does thh 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.

这篇关于为什么是复制构造函数而不是转换构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆