Chinaunix首页 | 论坛 | 博客
  • 博客访问: 544729
  • 博文数量: 92
  • 博客积分: 2511
  • 博客等级: 少校
  • 技术积分: 932
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-19 10:10
文章分类
文章存档

2011年(6)

2010年(27)

2009年(37)

2008年(22)

我的朋友

分类: C/C++

2008-10-19 11:06:51

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

1. 以下函数哪个是拷贝构造函数,为什么?
X::X(const X&);  
X::X(X);  

X::X(X&, int a=1);  //这里我和本文的作者观点不一至,没有const是非法的,因为无法

                    //把一个非const参数赋给一个临时变量,见《runminations on                     //c++》

X::X(X&, int a=1, b=2);  

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

3. 写出以下程序段的输出结果, 并说明为什么? 如果你都能回答无误的话,那么你已经对拷贝构造函数有了相当的了解。
#include  
#include  
   
struct X {  
  template  
  X( T& ) { std::cout << "This is ctor." << std::endl; }  
   
  template  
  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  

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

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

 

3. 拷贝构造函数不能由成员函数模版生成. 
struct X {  
  template  
  X( const T& ); // NOT copy ctor, T can't be X  
   
  template  
  operator=( const T& ); // NOT copy ass't, T can't be X  
};  
   

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

二 针对上面作者的讨论,理解更深了,但是下面我还是会给出一个一般的标准的实现和注意事项:
#include "stdafx.h"
#include "stdio.h"
#include
#include


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;
}





阅读(1004) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~