拷贝构造函数
- 只有单个形参,而且该形参是对本类型对象的引用(常用const修饰),这样的构造函数称为复制构造函数。
- 复制构造函数的三个用途:1)当定义一个新对象并用一个同类型的对象对它进行初始化时,将显示调用复制构造函数;2)将对象传递给函数(该函数的形参是非引用类型时)或从函数返回非引用类型对象时将隐式调用复制构造函数。
- 生成的复制构造函数:如果我们没有定义复制构造函数,系统会为我们生成一个。但与合成默认构造函数不同,只有当我们一个构造函数也没定义时,系统才会生成默 认构造函数,如果定义了一个构造函数,系统就不会生成默认构造函数了。但我们定义了其他构造函数,如果没有定义拷贝构造函数,系统则会为我们自动生成一个拷贝构造函数。
1. 以下函数哪个是拷贝构造函数,为什么?
- X::X(const X&);
- X::X(X);
- X::X(X&, int a=1);
- X::X(X&, int a=1, b=2);
2. 一个类中可以存在多于一个的拷贝构造函数吗?
3. 写出以下程序段的输出结果, 并说明为什么? 如果你都能回答无误的话,那么你已经对拷贝构造函数有了相当的了解。
- #include
- #include
- struct X {
- template<typename T>
- X( T& ) { std::cout << "This is ctor." << std::endl; }
- template<typename T>
- X& operator=( T& ) { std::cout << "This is ctor." << std::endl; }
- };
- void main() {
- X a(5);
- X b(10.5);
- X c = a;
- c = b;
- }
解答如下:
1. 对于一个类X,如果一个构造函数的第一个参数是下列之一:
a) X&
b) const X&
c) volatile X&
d) const volatile X&
且没有其他参数或其他参数都有默认值,那么这个函数是拷贝构造函数.
- X::X(const X&); //是拷贝构造函数
- X::X(X&, int=1); //是拷贝构造函数
2.类中可以存在超过一个拷贝构造函数,
- class X {
- public:
- X(const X&);
- X(X&); // OK
- };
注意,如果一个类中只存在一个参数为X&的拷贝构造函数,那么就不能使用const X或volatile X的对象实行拷贝初始化.
- class X {
- public:
- X();
- X(X&);
- };
- const X cx;
- X x = cx; // error (除非你自己定义过operator=相关重载函数,后面有例子说明)
如果一个类中没有定义拷贝构造函数,那么编译器会自动产生一个默认的拷贝构造函数.
这个默认的参数可能为X::X(const X&)或X::X(X&),由编译器根据上下文决定选择哪一个.
默认拷贝构造函数的行为如下:
默认的拷贝构造函数执行的顺序与其他用户定义的构造函数相同,执行先父类后子类的构造.
拷贝构造函数对类中每一个数据成员执行成员拷贝(memberwise Copy)的动作.
a)如果数据成员为某一个类的实例,那么调用此类的拷贝构造函数.
b)如果数据成员是一个数组,对数组的每一个执行按位拷贝.
c)如果数据成员是一个数量,如int,double,那么调用系统内建的赋值运算符对其进行赋值.
3. 拷贝构造函数不能由成员函数模版生成.
-
- struct X {
- template<typename T>
- X( const T& ); // NOT copy ctor, T can't be X
- template<typename T>
- operator=( const T& ); // NOT copy ass't, T can't be X
- };
原因很简单, 成员函数模版并不改变语言的规则,而语言的规则说,如果程序需要一个拷贝构造函数而你没有声明它,那么编译器会为你自动生成一个. 所以成员函数模版并不会阻止编译器生成拷贝构造函数, 赋值运算符重载也遵循同样的规则.(参见Effective C++ 3edition, Item45)
二 针对上面作者的讨论,理解更深了,但是下面我还是会给出一个一般的标准的实现和注意事项:
#include " stdio.h "
#include < iostream >
#include < string >
struct Test1
{
Test1() { }
Test1(int i) { id = i; }
Test1(const Test1& test)
{
id = test.id;
}
Test1& operator = (const Test1& test)
{
if(this == &test)
return *this;
id = test.id;
return *this;
}
int id;
} ;
class Test2
{
public:
Test2(){ m_pChar = NULL;}
Test2(char *pChar) { m_pChar = pChar;}
Test2(int num)
{
m_pChar = new char[num];
for(int i = 0; i< num; ++i)
m_pChar[i] = 'a';
m_pChar[num-1] = '/0';
}
Test2(const Test2& test)
{
char *pCharT = m_pChar;
m_pChar = new char[strlen(test.m_pChar)];
strcpy(m_pChar, test.m_pChar);
if(!pCharT)
delete []pCharT;
}
Test2& operator = (const Test2& test)
{
if(this == &test)
return *this;
char *pCharT = m_pChar;
m_pChar = new char[strlen(test.m_pChar)];
strcpy(m_pChar, test.m_pChar);
if(!pCharT)
delete []pCharT;
return *this;
}
private:
char *m_pChar;
} ;
int main( int argc, char * argv[])
{
const Test1 ts(1); // Test1()
const Test1* p_ts = &ts;
const Test1 ts2(ts); //Test(const Test1& test)
const Test1 ts3 = ts; //Test(const Test1& test)
Test1 ts4; ts4 = ts; //Test1& operator = (const Test1& test)
Test2 t(5);
Test2 t2(t);
Test2 t3 = t2;
Test2 t4; t4 = t;
return 0;
}
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>