为什么复制ctor不叫 [英] why copy ctor not called

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

问题描述

考虑以下计划:


#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.Subramanian

consider 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 output
in both g++ and VC++ Express Edition 2005
10
20
30
30

I thought the copy ctor would be called when we invoke a.add(b)
because Test::add() returns an object of Test type and here
I 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.

Thanks
V.Subramanian

推荐答案

On 11 ??29è?,????1ê±52·?,subramanian10 ... @ yahoo.com,India

< subramanian10 ... @ yahoo.comwrote:
On 11??29è?, ????1ê±52·?, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:

考虑以下程序:


#包括< iostream>


使用命名空间std;


class测试

{

public:

Test(int xx):x(xx){cout<< x<< ENDL; } $ / $

测试添加(const测试& obj);


测试(const测试& obj):x(obj.x)

{cout<< copy ctor << x<< ENDL; }


int x;


};


测试测试::添加(const测试) & obj)

{

返回x + obj.x;


}


int main()

{

测试a(10);

测试b(20);


cout<< a.add(b).x<< endl;


返回0;


}


这个程序编译好并产生以下内容在g ++和VC ++ Express Edition 2005中输出



10

20

30

30


我认为当我们调用a.add(b)

时会调用copy ctor,因为Test :: add()会返回一个对象测试类型和这里

我希望调用复制文件。


我不明白为什么没有调用复制文件。

它是否优化了?或者这是预期的行为。


请澄清。


谢谢

V.Subramanian
consider 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 output
in both g++ and VC++ Express Edition 2005
10
20
30
30

I thought the copy ctor would be called when we invoke a.add(b)
because Test::add() returns an object of Test type and here
I 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.

Thanks
V.Subramanian



你的函数添加使用referenece作为参数。它不会创建一个新的

临时对象。因此它不会调用复制构造函数。

your function add use referenece as argument. It does not create a new
temp object. So it does not call a copy constructor.


在2007-11-29 00:52:25 -0500,su ********* *****@yahoo.com,印度"

< su ************** @ yahoo.comsaid:
On 2007-11-29 00:52:25 -0500, "su**************@yahoo.com, India"
<su**************@yahoo.comsaid:

考虑以下程序:


#include< iostream>


using namespace std;


班级考试

{

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类型的对象,这里

我希望调用复制文件。


我不明白为什么没有调用复制文件。

它被优化了吗?或者这是预期的行为。
consider 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 output
in both g++ and VC++ Express Edition 2005
10
20
30
30

I thought the copy ctor would be called when we invoke a.add(b)
because Test::add() returns an object of Test type and here
I 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.



嗯...有趣。即使以下代码也不会调用复制构造函数:


void func()

{

测试a(10 );

测试b = a.add(a); //我希望这一行调用副本

ctor但是没有!

cout<< b.x<<结束;

}


我也添加了析构函数:


~Test(){cout< < 毁灭 << x<< ENDL; }


我的输出是这样的:


10

20

30 < br $> b $ b 30

毁灭30

毁灭20

毁灭10

行为似乎是这样的:当你从一个临时的

对象复制时,它只做一个按位复制并且不会调用临时的

对象''析构函数。


FYI,我正在使用g ++ 4.0.1。


-


-kira

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 copy
ctor but didn''t!
cout << b.x << endl;
}

And I added the destructor too:

~Test() { cout << "destroy " << x << endl; }

My output was this:

10
20
30
30
destroy 30
destroy 20
destroy 10

The behavior seems to be this: When you''re copying from a temporary
object, it just does a bitwise copy and does not call the temporary
object''s destructor.

FYI, I''m using g++ 4.0.1.

--

-kira


2007-11-29 01:34:49 -0500,Kira Yamato< ki ***** @ earthlink。 netsaid:
On 2007-11-29 01:34:49 -0500, Kira Yamato <ki*****@earthlink.netsaid:

2007-11-29 00:52:25 -0500,su ************** @ yahoo.com,印度"

< su ************** @ yahoo.comsaid:
On 2007-11-29 00:52:25 -0500, "su**************@yahoo.com, India"
<su**************@yahoo.comsaid:

>考虑以下程序:

#include< iostream>
使用命名空间std;

类测试
{
public:
Test(int xx):x(xx){cout<< x<< ENDL;测试添加(const Test& obj);

测试(const测试& obj):x(obj.x)
{cout< < copy ctor << x<< ENDL; }测试测试::添加(const测试和对象)
{
返回x + obj.x;
}
int main()
{
测试(10);
测试b(20);

cout<< a.add(b).x<< endl;

返回0;
}

这个程序编译很好,并在g ++和VC ++ Express Edition 2005中产生以下输出 10
20
30

我认为当我们调用a.add(b)
时会调用复制ctor因为Test :: add()返回一个Test类型的对象,在这里
我希望调用copy ctor。

我不明白为什么没有调用copy ctor。
是吗优化了吗?或者这是预期的行为。
>consider 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 output
in both g++ and VC++ Express Edition 2005
10
20
30
30

I thought the copy ctor would be called when we invoke a.add(b)
because Test::add() returns an object of Test type and here
I 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.



嗯...有趣。即使以下代码也不会调用复制构造函数:


void func()

{

测试a(10 );

测试b = a.add(a); //我希望这一行调用副本

ctor但是没有!

cout<< b.x<<结束;

}


我也添加了析构函数:


~Test(){cout< < 毁灭 << x<< ENDL; }


我的输出是这样的:


10

20

30 < br $> 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 copy
ctor but didn''t!
cout << b.x << endl;
}

And I added the destructor too:

~Test() { cout << "destroy " << x << endl; }

My output was this:

10
20
30
30
destroy 30
destroy 20
destroy 10

The behavior seems to be this: When you''re copying from a temporary
object, it just does a bitwise copy and does not call the temporary
object''s destructor.

FYI, I''m using g++ 4.0.1.



好​​的,我在g ++的联机帮助页中找到了这个:


-fno-elide-constructors

C ++标准允许实现省略创建一个tempo-

rary,它仅用于初始化相同

类型的另一个对象。指定此选项会禁用该优化,并且

强制G ++在所有情况下都调用复制构造函数。


因此,答案是它是C ++标准行为。


-


-kira

Ok, I found this in the manpages for g++:

-fno-elide-constructors
The C++ standard allows an implementation to omit creating a tempo-
rary which is only used to initialize another object of the same
type. Specifying this option disables that optimization, and
forces G++ to call the copy constructor in all cases.

So, the answer is that it is a C++ standard behavior.

--

-kira


这篇关于为什么复制ctor不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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