C ++ copy-construct构造和赋值问题 [英] C++ copy-construct construct-and-assign question

查看:173
本文介绍了C ++ copy-construct构造和赋值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C ++ Gotchas一书第56项的摘录:

Here is an extract from item 56 of the book "C++ Gotchas":


这是一个很简单的
初始化一个Y对象,用
三种不同的方式写,如同
它们是等价的。

It's not uncommon to see a simple initialization of a Y object written any of three different ways, as if they were equivalent.



Y a( 1066 ); 
Y b = Y(1066);
Y c = 1066;




事实上,这三个
初始化可能会导致
在同一目标代码中生成
,但它们不是等价的。
a的初始化称为
直接初始化,它正确的是

初始化通过
a直接调用Y :: Y(int)来实现。

In point of fact, all three of these initializations will probably result in the same object code being generated, but they're not equivalent. The initialization of a is known as a direct initialization, and it does precisely what one might expect. The initialization is accomplished through a direct invocation of Y::Y(int).

b和c的初始化是
更复杂。事实上,他们太
复杂。这些都是复制
初始化。在b的
初始化的情况下,我们请求
创建一个匿名临时的
类型Y,初始化为值
1066.然后我们使用这个匿名临时作为参数复制
构造函数为Y类初始化
b。最后,我们为
调用析构函数作为匿名临时。

The initializations of b and c are more complex. In fact, they're too complex. These are both copy initializations. In the case of the initialization of b, we're requesting the creation of an anonymous temporary of type Y, initialized with the value 1066. We then use this anonymous temporary as a parameter to the copy constructor for class Y to initialize b. Finally, we call the destructor for the anonymous temporary.

为了测试这个,我做了一个简单的类成员(节目附在结尾),结果令人惊讶。看起来对于c的情况,对象是由复制构造函数而不是在书中建议的。

To test this, I did a simple class with a data member (program attached at the end) and the results were surprising. It seems that for the case of c, the object was constructed by the copy constructor rather than as suggested in the book.

有人知道语言标准是否已经改变,这只是编译器的一个优化功能吗?我使用Visual Studio 2008.

Does anybody know if the language standard has changed or is this simply an optimisation feature of the compiler? I was using Visual Studio 2008.

代码示例:

#include <iostream>

class Widget
{
    std::string name;
public:
    // Constructor
    Widget(std::string n) { name=n; std::cout << "Constructing Widget " << this->name << std::endl; }
    // Copy constructor
    Widget (const Widget& rhs) { std::cout << "Copy constructing Widget from " << rhs.name << std::endl; }
    // Assignment operator
    Widget& operator=(const Widget& rhs) { std::cout << "Assigning Widget from " << rhs.name << " to " << this->name << std::endl; return *this; }
};

int main(void)
{
    // construct
    Widget a("a");
    // copy construct
    Widget b(a);
    // construct and assign
    Widget c("c"); 
    c = a;
    // copy construct!
    Widget d = a;
    // construct!
    Widget e = "e";
    // construct and assign
    Widget f = Widget("f");

    return 0;
}

输出:

Constructing Widget a

Copy constructing Widget from a

Constructing Widget c
Assigning Widget from a to c

Copy constructing Widget from a

Constructing Widget e

Constructing Widget f
Copy constructing Widget from f

我最感到惊讶的是构建d和e的结果。确切地说,我期待创建一个空对象,然后创建一个对象并将其分配给空对象。在实践中,对象是由复制构造函数创建的。

I was most surprised by the results of constructing d and e. To be precise, I was expecting an empty object to be created, and then an object to be created and assigned to the empty object. In practice, the objects were created by the copy constructor.

推荐答案

语法

X a = b;

其中a和b是X类型,无论什么变体,例如:

where a and b are of type X has always meant copy construction. Whatever variants, such as:

X a = X();

,没有分配,从来没有。构造和赋值将类似于:

are used, there is no assignment going on, and never has been. Construct and assign would be something like:

X a;
a = X();

这篇关于C ++ copy-construct构造和赋值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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