复制构造函数和赋值运算符
我编写了以下程序来测试何时调用复制构造函数以及何时调用赋值运算符:
#include
class Test
{
public:
Test() :
iItem (0)
{
std::cout << "This is the default ctor" << std::endl;
}
Test (const Test& t) :
iItem (t.iItem)
{
std::cout << "This is the copy ctor" << std::endl;
}
~Test()
{
std::cout << "This is the dtor" << std::endl;
}
const Test& operator=(const Test& t)
{
iItem = t.iItem;
std::cout << "This is the assignment operator" << std::endl;
return *this;
}
private:
int iItem;
};
int main()
{
{
Test t1;
Test t2 = t1;
}
{
Test t1;
Test t2 (t1);
}
{
Test t1;
Test t2;
t2 = t1;
}
}
这会产生以下输出(仅添加空行以使其更易于理解):
doronw@DW01:~$ ./test This is the default ctor This is the copy ctor This is the dtor This is the dtor This is the default ctor This is the copy ctor This is the dtor This is the dtor This is the default ctor This is the default ctor This is the assignment operator This is the dtor This is the dtor
第二个和第三个set 的行为符合预期,但在第一个 set 中,即使使用了赋值运算符,也会调用复制构造函数。
这种行为是 C++ 标准的一部分还是只是一个聪明的编译器优化(我正在使用 gcc 4.4.1)
I wrote the following program to test when the copy constructor is called and when the assignment operator is called:
#include
class Test
{
public:
Test() :
iItem (0)
{
std::cout << "This is the default ctor" << std::endl;
}
Test (const Test& t) :
iItem (t.iItem)
{
std::cout << "This is the copy ctor" << std::endl;
}
~Test()
{
std::cout << "This is the dtor" << std::endl;
}
const Test& operator=(const Test& t)
{
iItem = t.iItem;
std::cout << "This is the assignment operator" << std::endl;
return *this;
}
private:
int iItem;
};
int main()
{
{
Test t1;
Test t2 = t1;
}
{
Test t1;
Test t2 (t1);
}
{
Test t1;
Test t2;
t2 = t1;
}
}
This results in the following output (just added empy lines to make it more understandable):
doronw@DW01:~$ ./test This is the default ctor This is the copy ctor This is the dtor This is the dtor This is the default ctor This is the copy ctor This is the dtor This is the dtor This is the default ctor This is the default ctor This is the assignment operator This is the dtor This is the dtor
The second and third set behave as expected, but in the first set the copy constructor is called even though the assignment operator is used.
Is this behaviour part of the C++ standard or just a clever compiler optimization (I am using gcc 4.4.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个测试用例中没有使用赋值运算符。它只是使用称为“复制初始化”的初始化形式。复制初始化在初始化对象时不考虑显式构造函数。
复制初始化用于与隐式转换相对应的情况,其中不显式启动转换,如函数参数传递和从函数返回。
No assignment operator is used in the first test-case. It just uses the initialization form called "copy initialization". Copy initialization does not consider explicit constructors when initializing the object.
Copy initialization is used in those cases that correspond to implicit conversions, where one does not explicitly kick off a conversion, as in function argument passing, and returning from a function.
C++ 标准 8.5/12
C++ standard 8.5/12
您的第一组是根据 C++ 标准,而不是由于某些优化。
第 12.8 节 (
[class.copy]
) C++ 标准 给出了类似的示例:最后一行将是与您的情况匹配的行。
Your first set is according to the C++ standard, and not due to some optimization.
Section 12.8 (
[class.copy]
) of the C++ standard gives a similar example:The last line would be the one matching your case.