c++ 达人乱进

大漠张三

知名会员
VIP
注册
2003-04-18
消息
9,432
荣誉分数
84
声望点数
158
class whatever {
whatever()
~whatever()
whatever(whatever & a)
}

whatever a ();
whatever b ();

a = b; //这个call的是什么?copy constructor?

whatever b = a //这个呢?还是copy constructor?
 
a=b -> assignment
whatever b=a -> copy consturctor
 
最初由 Rabbit 发布
a=b -> assignment
whatever b=a -> copy consturctor



so what does a=b do?

say in the whatever class definition
private {
char* name;
int studentNum;
}
 
http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

Difference between copy constructor and assignment

A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler:

1. There is no need to test to see if it is being initialized from itself.
2. There is no need to clean up (eg, delete) an existing value (there is none).
3. A reference to itself is not returned.
 
both is "=" overload. you can put the copy constucotr inside "=" overload
 
最初由 大漠张三 发布




so what does a=b do?

say in the whatever class definition
private {
char* name;
int studentNum;
}

copy construtor
whatever::whatever(whatever& a)
{
strcopy(name,a.name);
studenNum=a.student;
}

"=" overload:
{
call copy constutor
}
 
最初由 Rabbit 发布
http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

Difference between copy constructor and assignment

A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler:

1. There is no need to test to see if it is being initialized from itself.
2. There is no need to clean up (eg, delete) an existing value (there is none).
3. A reference to itself is not returned.


如果我的copy constructor只是 whatever(whatever& a) {}呢?is the default copy constructor still going to make a deep copy?
 
C++ uses the default copy constructor only if there is no copy constructor defined for the class. Given that, you have to put sth in {...} because C++ doesn't use the default copy any more in your case.

BTW, the default copy constructor only makes a shallow copy. Don't write a copy constructor if shallow copies are ok for you. In addition, if you need deep copy, you have to write your own copy constructor.

Good luck!

最初由 大漠张三 发布



如果我的copy constructor只是 whatever(whatever& a) {}呢?is the default copy constructor still going to make a deep copy?
 
最初由 西出阳关 发布
C++ uses the default copy constructor only if there is no copy constructor defined for the class. Given that, you have to put sth in {...} because C++ doesn't use the default copy any more in your case.

BTW, the default copy constructor only makes a shallow copy. Don't write a copy constructor if shallow copies are ok for you. In addition, if you need deep copy, you have to write your own copy constructor.

Good luck!



if i declare my own copy constructor to do a deep copy; but i do not overload my "=" operator.
when i type b=a, when a and b are both objects, do i invoke the copy constructor or not?
 
最初由 大漠张三 发布



if i declare my own copy constructor to do a deep copy; but i do not overload my "=" operator.
when i type b=a, when a and b are both objects, do i invoke the copy constructor or not?

a = b; //default operator =
whatever c = b; //copy constructor
 
后退
顶部