天天看點

C++類的複制構造函數

複制構造函數的形式為:

class ClassName
{
...

ClassName(ClassName&)
{
...
}
或
ClassName(const ClassName&)
{
...
}
...
};
           

如果沒有定義複制構造函數,則編譯器會為該類自動生成複制構造函數。如果使用者自定義了任何一個類型的複制構造函數,則不再生成預設的複制構造函數。

生成的預設構造函數隻是簡單地進行值的複制。對于包含指向動态配置設定的記憶體空間的指針變量來說,複制構造函數隻是将指針變量的值複制給新的變量,而使得兩個對象中的變量指向同一個記憶體空間。這就會造成資料之間的耦合,甚至會出現多次删除同一個指針的問題,會産生嚴重的錯誤。是以,對于包含這類成員變量的類來說,其複制構造函數需要自定義,需要為新的對象中的變量配置設定新的記憶體空間,并根據源對象的變量的值填充該記憶體空間。與以值複制相比,這種類型的複制成為“深拷貝”,而以值的複制的形式稱為“淺拷貝”。

/*****************************************
 * copy_constructor.cpp                  *
 *                                       *
 * C++類的複制構造函數(淺拷貝帶來的多次釋放記憶體 *
 * 錯誤                                   *
 ****************************************/


class Test
{
private:
  int i;
  int *p;

public:
  Test()
  {
    i = ;
    p = new int();
  }

  //淺拷貝
  Test(const Test& t)
  {
    i = t.i;
    p = t.p;
  }

  ~Test()
  {
    delete p;
  }

};

int main()
{
  Test t;
  Test t1(t);

  return ;
}
           
C++類的複制構造函數
/****************************************
 * copy_constructor_2.cpp               *
 *                                      *
 * C++類的複制構造函數(深拷貝)          *
 ****************************************/

#include <iostream>
using namespace std;

class Test
{
private:
  int i;
  int *p;

public:
  Test()
  {
    i = ;
    p = new int();
  }

  //深拷貝
  Test(const Test& t)
  {
    i = t.i;
    p = new int(*t.p);
  }

  ~Test()
  {
    delete p;
  }

public:
  void Print()
  {
    cout<<"i = "<<i<<","<<"*p = "<<*p<<endl;
  }
};

int main()
{
  Test t;
  t.Print();
  Test t1(t);
  t1.Print();
  return ;
}
           
C++類的複制構造函數

複制構造函數應用于以下三種情況:

  • 使用一個對象去初始化同類的另一個對象
  • 在類的對象作為函數參數的情況下,當函數調用時,其複制構造函數被調用
  • 當類的對象作為函數的傳回值時,當函數傳回時,該類的複制構造函數被調用。
/****************************************
 * copy_constructor_3.cpp               *
 *                                      *
 * 複制構造函數應用的三種情況           *
 ****************************************/

#include <iostream>
using namespace std;

class A
{
private:
  static int id;
public:
  int value;
public:
  A(int value)
  {
    id++;
    this->value = value;
  }

  A(const A& a)
  {
    value = a.value;
    id++;
    cout<<"複制構造函數(id)"<<id<<"被調用"<<endl;
  }

};

int A::id = ;

void UsingAAsPara(A a)
{
}

A UsingAAsReturn()
{
  A a();
  return a;
}

int main()
{
  A a();

  cout<<"A b(a): ";
  A b(a);
  cout<<"A c = a:";
  A c = a;
  cout<<"UsingAAsPara(c)";
  UsingAAsPara(c);
  cout<<"UsingAAsReturn()";
  cout<<UsingAAsReturn().value<<endl;
  return ;
}
           
C++類的複制構造函數

使用

g++

編譯器發現第三種情況下并沒有調用複制構造函數,而是使用的帶參數

valu

e的構造函數。可見,

g++

編譯器對這種情況進行了優化,見下面的标準中的描述。

When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization) This elision of copy operations is permitted in the following circumstances (which may be combined
to eliminate multiple copies):
— in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value
— when a temporary class object that has not been bound to a reference () would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy
[ Example:
class Thing {
    public :
        Thing ();
        ~ Thing ();
        Thing ( const Thing &);
};
Thing f () {
    Thing t;
    return t;
}
Thing t2 = f();
Here the criteria for elision can be combined to eliminate two calls to the copy constructor of class Thing: the copying of the local automatic object t into the temporary object for the return value of function f() and the copying of that temporary object into object t2. Effectively, the construction of the local object t can be viewed as directly initializing the global object t2, and that object’s destruction will occur at program exit. —end example ]
           

參考文獻

https://www.coursera.org/course/pkupop

繼續閱讀