r******9 发帖数: 129 | 1 关于什么时候调用copy constructor的问题, 看下面的例子
class cType
{
public:
cType(){}
cType(const cType& obj)
{
cout << "copy constructor is called" << endl;
}
cType namedObj()
{
cType d;
return d;
}
cType pointer()
{
cType *d = new cType();
return *d;
}
};
int main()
{
cType obj;
obj.namedObj(); //这个就不会调用copy constructor
obj.pointer(); //这个就会调用 copy constructor
}
为什么会有这个区别呢? 不是说只要返回值不是reference 就会copy么?
多谢指教 |
p***o 发帖数: 1252 | 2 It's up to the compiler and your program behavior should not depend on
whether it's called or not.
【在 r******9 的大作中提到】 : 关于什么时候调用copy constructor的问题, 看下面的例子 : class cType : { : public: : cType(){} : cType(const cType& obj) : { : cout << "copy constructor is called" << endl; : } :
|
g*********s 发帖数: 1782 | 3 so in case 1, how did the compiler work?
【在 p***o 的大作中提到】 : It's up to the compiler and your program behavior should not depend on : whether it's called or not.
|
P********e 发帖数: 2610 | 4 I think copy ctr will be called in both cases.
【在 g*********s 的大作中提到】 : so in case 1, how did the compiler work?
|
g*********s 发帖数: 1782 | 5 someone mentioned recently the compiler would be smart, in a thread i
opened.
【在 P********e 的大作中提到】 : I think copy ctr will be called in both cases.
|
a*****i 发帖数: 268 | 6 第一个情况return value optimization可以起作用,第二个情况不会。而且第二个情
况有memory leak。 |
B*******g 发帖数: 1593 | 7 1 ‘优化’了吧 VC默认好像是这样的
【在 r******9 的大作中提到】 : 关于什么时候调用copy constructor的问题, 看下面的例子 : class cType : { : public: : cType(){} : cType(const cType& obj) : { : cout << "copy constructor is called" << endl; : } :
|
o*******0 发帖数: 699 | 8 调了一下,学到一点东西。
cType namedObj()
{
cType d;
return d;
}
是对的,copy constructor 在返回时调用。
cType& namedObj()
{
cType d;
return d;
}
是错的,返回完reference再调用copy constructor
cType pointer()
{
cType *d = new cType();
return *d;
}
和
cType& pointer()
{
cType *d = new cType();
return *d;
}
都是对的。虽然copy constructor 调用时间不同。 |
X****r 发帖数: 3557 | 9 1) correct, although the compiler may optimize it out in some cases.
2) undefined -- you can't return a reference of a local variable.
3) memory leak -- local pointer never freed.
4) very likely memory leak, unless you convert the reference to
pointer and free that pointer later (hacky).
【在 o*******0 的大作中提到】 : 调了一下,学到一点东西。 : cType namedObj() : { : cType d; : return d; : } : 是对的,copy constructor 在返回时调用。 : cType& namedObj() : { : cType d;
|
o*******0 发帖数: 699 | 10 The point is not about memory leak (which is true), but returning local
vairable (instance of object) will trigger copy contructor to copy it to the
caller stack. Not sure how compiler can optimize it away.
Thanks |
X****r 发帖数: 3557 | 11 Someone already mentioned in this thread: return value optimization.
This is a specific clause allows it:
12.8 Copying class objects
15 Whenever a class object is copied and the original object and
the copy have the same type, if the implementation can prove that
either the original object or the copy will never again be used
except as the result of an implicit destructor call, an
implementation is permitted to treat the original and the copy as
two different ways of referring to the same object and not perform
a copy at all.
the
【在 o*******0 的大作中提到】 : The point is not about memory leak (which is true), but returning local : vairable (instance of object) will trigger copy contructor to copy it to the : caller stack. Not sure how compiler can optimize it away. : Thanks
|
N***m 发帖数: 4460 | 12 Effective c++ 上还专门讲过 RVO
【在 X****r 的大作中提到】 : Someone already mentioned in this thread: return value optimization. : This is a specific clause allows it: : 12.8 Copying class objects : 15 Whenever a class object is copied and the original object and : the copy have the same type, if the implementation can prove that : either the original object or the copy will never again be used : except as the result of an implicit destructor call, an : implementation is permitted to treat the original and the copy as : two different ways of referring to the same object and not perform : a copy at all.
|
N***m 发帖数: 4460 | |