由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - copy constructor 问题
相关主题
问一个 copy constructor 的问题 (C++)包含指针的类和vector的问题
问一个关于copy constructor的菜鸟问题。。。。。 (转载)Python problem on 64 bit Linux
子类的copy constructor怎么写C++的一个mutex问题
C++问题,confusing...const reference in copy constructor
[合集] 关于C++ default copy constructorC++的一个小疑问,求解惑
copy constructor问题。C++ operator = overloading用copy & swap有啥优点
[合集] C++问题(copy constructor)C#复制栈是反序的?
C++ vector一个小问题
相关话题的讨论汇总
话题: ctype话题: copy话题: 调用话题: return
进入Programming版参与讨论
1 (共1页)
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
13
你new一下就赋予了class以生命力。
1 (共1页)
进入Programming版参与讨论
相关主题
一个小问题[合集] 关于C++ default copy constructor
C++ optimization questioncopy constructor问题。
菜鸟请教smart pointer[合集] C++问题(copy constructor)
a simple question about constructorC++ vector
问一个 copy constructor 的问题 (C++)包含指针的类和vector的问题
问一个关于copy constructor的菜鸟问题。。。。。 (转载)Python problem on 64 bit Linux
子类的copy constructor怎么写C++的一个mutex问题
C++问题,confusing...const reference in copy constructor
相关话题的讨论汇总
话题: ctype话题: copy话题: 调用话题: return