当由同一类的functionreturning对象初始化时,不会调用复制构造函数 [英] Copy constructor doesn't get called when initialized by functionreturning object of same class
问题描述
我对下面的一段代码感到有点困惑:
---------------------- -------------------------------------------------- ----------
#include< iostream>
使用命名空间std;
class Test {
public:
Test(){cout<<" Cons\\\
" ;;}
Test(Test& a) {cout<<" copy cons\\\
";}
};
测试乐趣()
{
返回测试();
}
int main()
{
cout<<""第一种初始化方式\ n" ;;
测试t1;
测试t2 = t1;
cout<<" \ n第二种初始化方式\ n" ;;
测试t3 = fun();
返回0;
}
OUTPUT(在CC编译器上编译时):
第一种初始方式
缺点
复制缺点
第二种初始化方式
缺点
----------------------------- -------------------------------------------------- -----
我很感兴趣为什么第二次初始化不会调用副本
构造函数?
Aren''我们将fun()调用返回的临时对象传递给
t3进行复制吗?
I am bit puzzled at the following piece of code I tried:
----------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout<<"Cons\n";}
Test(Test& a) { cout<<"Copy cons\n";}
};
Test fun()
{
return Test();
}
int main()
{
cout<<"First way of initialization\n";
Test t1;
Test t2 = t1;
cout<<"\nSecond way of initialization\n";
Test t3 = fun();
return 0;
}
OUTPUT (when compiled on CC compiler) :
First way of initialization
Cons
Copy cons
Second way of initialization
Cons
------------------------------------------------------------------------------------
I am intrigued why second initialization doesn''t call copy
constructor ?
Aren''t we passing the temporary object returned by fun() call to the
t3 for copying ?
推荐答案
abhash写道:
abhash wrote:
我对下面的一段代码感到有点困惑:
I am bit puzzled at the following piece of code I tried:
< ; snip>
我很感兴趣为什么第二次初始化不会调用复制
构造函数?
我们不是吗?将fun()调用返回的临时对象传递给
t3进行复制?
<snip>
I am intrigued why second initialization doesn''t call copy
constructor ?
Aren''t we passing the temporary object returned by fun() call to the
t3 for copying ?
Google forC ++ RVO
-
Ian Collins。
Google for "C++ RVO"
--
Ian Collins.
6月12日上午11:05,abhash< amit.abh ... @ gmail.comwrote:
On Jun 12, 11:05 am, abhash <amit.abh...@gmail.comwrote:
我对以下我试过的代码感到有点困惑:
----------------------- -------------------------------------------------- ---------
#include< iostream>
使用命名空间std;
class Test {
public:
Test(){cout<<" Cons\\\
" ;;}
Test(Test& a){ cout<<"复制cons\\\
" ;;}
I am bit puzzled at the following piece of code I tried:
----------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout<<"Cons\n";}
Test(Test& a) { cout<<"Copy cons\n";}
这不是复制构造函数的正确语法。应该是这样的:
测试(const测试& a)
This is not the right syntax for copy constructor. It should be like:
Test(const Test& a)
>
};
测试乐趣()
{
返回测试();
}
int main()
{
cout<<""第一种初始化方式\ n" ;
测试t1;
测试t2 = t1;
cout<<" \ n第二种初始化方式\\ n;
测试t3 =有趣();
返回0;
}
>
OUTPUT(在CC编译器上编译时):
第一种初始化方式
缺点
复制cons
第二种初始方式
缺点
------------------ -------------------------------------------------- ----------------
我很感兴趣为什么第二次初始化不会调用副本
构造函数?
我们不能通过临时物体ret由fun()打电话给
t3进行复制?
>
};
Test fun()
{
return Test();
}
int main()
{
cout<<"First way of initialization\n";
Test t1;
Test t2 = t1;
cout<<"\nSecond way of initialization\n";
Test t3 = fun();
return 0;
}
OUTPUT (when compiled on CC compiler) :
First way of initialization
Cons
Copy cons
Second way of initialization
Cons
------------------------------------------------------------------------------------
I am intrigued why second initialization doesn''t call copy
constructor ?
Aren''t we passing the temporary object returned by fun() call to the
t3 for copying ?
2008-06-12 10:13:56 +0200,sumsin< su ******* @ gmail.comsaid:
On 2008-06-12 10:13:56 +0200, sumsin <su*******@gmail.comsaid:
6月12日上午11:05,abhash< amit.abh ... @ gmail.comwrote:
On Jun 12, 11:05 am, abhash <amit.abh...@gmail.comwrote:
>我对下面的一段代码感到有点困惑:
----------------------- -------------------------------------------------- ---------
>I am bit puzzled at the following piece of code I tried:
----------------------------------------------------------------------------------
#include
#include
>>
< iostream>
使用命名空间std;
类测试{
public:
Test(){ cout<<<" Cons\\\
";}
测试(测试& a){cout<<"复制cons\ n" ;;}
>>
<iostream>
using namespace std;
class Test {
public:
Test() { cout<<"Cons\n";}
Test(Test& a) { cout<<"Copy cons\n";}
这不是复制构造函数的正确语法。应该是这样的:
测试(const测试& a)
This is not the right syntax for copy constructor. It should be like:
Test(const Test& a)
虽然有副本是不寻常的通过非const引用获取其参数
的构造函数,它仍然是一个复制构造函数。
-
Pete
Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者
标准C ++库扩展:教程和参考
www.petebecker.com/tr1book )
While it''s unusual to have a copy constructor that takes its argument
by non-const reference, it is, nonetheless, a copy constructor.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
这篇关于当由同一类的functionreturning对象初始化时,不会调用复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!