本文介绍了为什么复制ctor不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 考虑以下计划: #include< iostream> 使用命名空间std; class Test { public: Test(int xx):x(xx){cout<< x<< ENDL; } $ / $ 测试添加(const测试& obj); 测试(const测试& obj):x(obj.x) {cout<< copy ctor << x<< ENDL; } int x; }; Test Test :: add(const Test& obj) { 返回x + obj.x; } int main() { 测试a(10); 测试b(20); cout< ;< a.add(b).x<<结束; 返回0; } 此程序编译正常并产生以下输出 g ++和VC ++ Express Edition 2005中的 10 20 30 30 我认为当我们调用a.add(b) 时会调用copy ctor,因为Test :: add()返回一个Test类型的对象,这里 我希望调用复制文件。 我不明白为什么没有调用复制文件。 它被优化了吗?或者这是预期的行为。 请澄清。 谢谢 V.Subramanianconsider the following program:#include <iostream>using namespace std;class Test{public:Test(int xx) : x(xx) { cout << x << endl; }Test add(const Test & obj);Test(const Test & obj) : x(obj.x){ cout << "copy ctor " << x << endl; }int x;};Test Test::add(const Test & obj){return x + obj.x;}int main(){Test a(10);Test b(20);cout << a.add(b).x << endl;return 0;}This program compiles fine and produces the following outputin both g++ and VC++ Express Edition 200510203030I thought the copy ctor would be called when we invoke a.add(b)because Test::add() returns an object of Test type and hereI expected the copy ctor to be called.I do not understand why the copy ctor is not called.Is it optimized away ? Or this is the expected behaviour.Kindly clarify.ThanksV.Subramanian推荐答案 你的函数添加使用referenece作为参数。它不会创建一个新的 临时对象。因此它不会调用复制构造函数。your function add use referenece as argument. It does not create a newtemp object. So it does not call a copy constructor. 嗯...有趣。即使以下代码也不会调用复制构造函数: void func() { 测试a(10 ); 测试b = a.add(a); //我希望这一行调用副本 ctor但是没有! cout<< b.x<<结束; } 我也添加了析构函数: ~Test(){cout< < 毁灭 << x<< ENDL; } 我的输出是这样的: 10 20 30 b $ b 30 毁灭30 毁灭20 毁灭10 行为似乎是这样的:当你从一个临时的 对象复制时,它只做一个按位复制并且不会调用临时的 对象''析构函数。 FYI,我正在使用g ++ 4.0.1。 - -kiraHmm... Interesting. Even the following code won''t call the copy constructor:void func(){Test a(10);Test b = a.add(a); // I expected this line to invoke copyctor but didn''t!cout << b.x << endl;}And I added the destructor too:~Test() { cout << "destroy " << x << endl; }My output was this:10203030destroy 30destroy 20destroy 10The behavior seems to be this: When you''re copying from a temporaryobject, it just does a bitwise copy and does not call the temporaryobject''s destructor.FYI, I''m using g++ 4.0.1.---kira 嗯...有趣。即使以下代码也不会调用复制构造函数: void func() { 测试a(10 ); 测试b = a.add(a); //我希望这一行调用副本 ctor但是没有! cout<< b.x<<结束; } 我也添加了析构函数: ~Test(){cout< < 毁灭 << x<< ENDL; } 我的输出是这样的: 10 20 30 b $ b 30 毁灭30 毁灭20 毁灭10 行为似乎是这样的:当你从一个临时的 对象复制时,它只做一个按位复制并且不会调用临时的 对象''析构函数。 仅供参考,我正在使用g ++ 4.0.1。Hmm... Interesting. Even the following code won''t call the copy constructor:void func(){ Test a(10); Test b = a.add(a); // I expected this line to invoke copyctor but didn''t! cout << b.x << endl;}And I added the destructor too: ~Test() { cout << "destroy " << x << endl; }My output was this:10203030destroy 30destroy 20destroy 10The behavior seems to be this: When you''re copying from a temporaryobject, it just does a bitwise copy and does not call the temporaryobject''s destructor.FYI, I''m using g++ 4.0.1. 好的,我在g ++的联机帮助页中找到了这个: -fno-elide-constructors C ++标准允许实现省略创建一个tempo- rary,它仅用于初始化相同 类型的另一个对象。指定此选项会禁用该优化,并且 强制G ++在所有情况下都调用复制构造函数。 因此,答案是它是C ++标准行为。 - -kiraOk, I found this in the manpages for g++:-fno-elide-constructorsThe C++ standard allows an implementation to omit creating a tempo-rary which is only used to initialize another object of the sametype. Specifying this option disables that optimization, andforces G++ to call the copy constructor in all cases.So, the answer is that it is a C++ standard behavior.---kira 这篇关于为什么复制ctor不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!