C++拷贝构造函数的几个细节








// Testing.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <iostream> 


using namespace std;


class baseClass
{
public:
baseClass()
{
cout << "基类构造函数被调用" <<endl;
}
virtual ~baseClass()
{
cout << "基类的析构函数应该是虚函数, 子类才能被正确析构" <<endl;
}
};


class Test1:public baseClass
{
public:
Test1()
{
cout << "子类构造函数被调用" <<endl;
}


Test1(char* pChar)
{
m_pChar = pChar;
cout << "子类构造函数被调用" <<endl;
}


~Test1()
{
cout << "子类析构函数被调用" <<endl;
}
Test1(const Test1 &t)
{
char* pTemp = m_pChar;


m_pChar = new char[5];
strcpy(m_pChar, t.m_pChar);


if(!pTemp)
delete []pTemp;


cout << "拷贝构造函数被调用,重新分配内存资源" <<endl;
}


Test1& operator =(const Test1 &t)
{
if(this == &t)
return *this;


char* pTemp = m_pChar;


m_pChar = new char[5];
strcpy(m_pChar, t.m_pChar);


if(!pTemp)
delete []pTemp;


cout << "操作符重载函数被调用,重新分配内存资源" <<endl;


}
private:
char* m_pChar;
};


void foo(Test1 b)
{


}
int _tmain(int argc, _TCHAR* argv[])
{
// 测试虚析构函数
cout << " 测试构造 虚析构函数.........................................." <<endl;
baseClass *b = new Test1();
delete b;


// 测试拷贝构造函数
cout << " 测试拷贝构造函数.........................................." <<endl;
Test1 sub("aaaa");
Test1 sub2 = sub;
Test1 sub3(sub);
foo(sub);


// 测试操作符重载函数
cout << " 测试操作符重载函数.........................................." <<endl;
Test1 sub4("bbbb");
Test1 sub5;
sub5 = sub4;


return 0;
}











C++拷贝构造函数的几个细节

C++拷贝构造函数的几个细节(转自:http://grantren.javaeye.com/blog/43289)

一 拷贝构造函数是C++最基础的概念之一,大家自认为对拷贝构造函数了解么?请大家先回答一下三个问题:

1. 以下函数哪个是拷贝构造函数,为什么?

  1. X::X(const X&);   
  2. X::X(X);   
  3. X::X(X&, int a=1);   
  4. X::X(X&, int a=1, b=2);  

 2. 一个类中可以存在多于一个的拷贝构造函数吗?

3. 写出以下程序段的输出结果, 并说明为什么? 如果你都能回答无误的话,那么你已经对拷贝构造函数有了相当的了解。

  1. #include    
  2. #include    
  3.   
  4. struct X {   
  5.   template<typename T>   
  6.   X( T& ) { std::cout << "This is ctor." << std::endl; }   
  7.   
  8.   template<typename T>   
  9.     X& operator=( T& ) { std::cout << "This is ctor." << std::endl; }   
  10. };   
  11.   
  12. void main() {   
  13.   X a(5);   
  14.   X b(10.5);   
  15.   X c = a;   
  16.   c = b;   
  17. }  

 

解答如下:

1. 对于一个类X,如果一个构造函数的第一个参数是下列之一:
a) X&
b) const X&
c) volatile X&
d) const volatile X&
且没有其他参数或其他参数都有默认值,那么这个函数是拷贝构造函数. 

  1. X::X(const X&);  //是拷贝构造函数   
  2. X::X(X&, int=1); //是拷贝构造函数  

 2.类中可以存在超过一个拷贝构造函数, 

  1. class X {      
  2. public:      
  3.   X(const X&);      
  4.   X(X&);            // OK   
  5. };  

注意,如果一个类中只存在一个参数为X&的拷贝构造函数,那么就不能使用const X或volatile X的对象实行拷贝初始化.

  1. class X {   
  2. public:   
  3.   X();   
  4.   X(X&);   
  5. };   
  6.     
  7. const X cx;   
  8. X x = cx;    // error   

如果一个类中没有定义拷贝构造函数,那么编译器会自动产生一个默认的拷贝构造函数.
这个默认的参数可能为X::X(const X&)或X::X(X&),由编译器根据上下文决定选择哪一个.

默认拷贝构造函数的行为如下:
 默认的拷贝构造函数执行的顺序与其他用户定义的构造函数相同,执行先父类后子类的构造.
 拷贝构造函数对类中每一个数据成员执行成员拷贝(memberwise Copy)的动作.
 a)如果数据成员为某一个类的实例,那么调用此类的拷贝构造函数.
 b)如果数据成员是一个数组,对数组的每一个执行按位拷贝. 
 c)如果数据成员是一个数量,如int,double,那么调用系统内建的赋值运算符对其进行赋值.

 

3.  拷贝构造函数不能由成员函数模版生成. 

  1. struct X {   
  2.     template<typename T>   
  3.     X( const T& );    // NOT copy ctor, T can't be X   
  4.   
  5.     template<typename T>   
  6.     operator=( const T& );  // NOT copy ass't, T can't be X   
  7. };   
  8.   

原因很简单, 成员函数模版并不改变语言的规则,而语言的规则说,如果程序需要一个拷贝构造函数而你没有声明它,那么编译器会为你自动生成一个. 所以成员函数模版并不会阻止编译器生成拷贝构造函数, 赋值运算符重载也遵循同样的规则.(参见Effective C++ 3edition, Item45)


二 针对上面作者的讨论,理解更深了,但是下面我还是会给出一个一般的标准的实现和注意事项:
#include "stdafx.h"
#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值