C++ 拷贝构造函数

  • 拷贝构造函数

    拷贝构造函数是通过用相同的类,它已先前创建的一个目的初始化它创建了一个对象的构造。复制构造函数用于-
    • 从另一个相同类型的对象初始化一个对象。
    • 复制对象以将其作为参数传递给函数。
    • 复制对象以从函数返回它。
    如果没有在类中定义副本构造函数,则编译器本身将定义一个副本构造函数。如果该类具有指针变量并具有一些动态内存分配,则必须具有副本构造函数。复制构造函数的最常见形式如下所示:
    
    classname (const classname &obj) {
       // body of constructor
    }
    
    此处,obj是对用于初始化另一个对象的对象的引用。
    
    #include <iostream>
    
    using namespace std;
    
    class Line {
    
       public:
          int getLength( void );
          Line( int len );             // simple constructor
          Line( const Line &obj);  // copy constructor
          ~Line();                     // destructor
    
       private:
          int *ptr;
    };
    
    // Member functions definitions including constructor
    Line::Line(int len) {
       cout << "Normal constructor allocating ptr" << endl;
       
       // allocate memory for the pointer;
       ptr = new int;
       *ptr = len;
    }
    
    Line::Line(const Line &obj) {
       cout << "Copy constructor allocating ptr." << endl;
       ptr = new int;
       *ptr = *obj.ptr; // copy the value
    }
    
    Line::~Line(void) {
       cout << "Freeing memory!" << endl;
       delete ptr;
    }
    
    int Line::getLength( void ) {
       return *ptr;
    }
    
    void display(Line obj) {
       cout << "Length of line : " << obj.getLength() << endl;
    }
    
    // Main function for the program
    int main() {
       Line line(10);
    
       display(line);
    
       return 0;
    }
    
    尝试一下
    编译并执行上述代码后,将产生以下结果-
    
    Normal constructor allocating ptr
    Copy constructor allocating ptr.
    Length of line : 10
    Freeing memory!
    Freeing memory!
    
    让我们看一下相同的示例,但是稍作改动即可使用相同类型的现有对象创建另一个对象-
    
    #include <iostream>
    
    using namespace std;
    
    class Line {
       public:
          int getLength( void );
          Line( int len );             // simple constructor
          Line( const Line &obj);  // copy constructor
          ~Line();                     // destructor
    
       private:
          int *ptr;
    };
    
    // Member functions definitions including constructor
    Line::Line(int len) {
       cout << "Normal constructor allocating ptr" << endl;
       
       // allocate memory for the pointer;
       ptr = new int;
       *ptr = len;
    }
    
    Line::Line(const Line &obj) {
       cout << "Copy constructor allocating ptr." << endl;
       ptr = new int;
       *ptr = *obj.ptr; // copy the value
    }
    
    Line::~Line(void) {
       cout << "Freeing memory!" << endl;
       delete ptr;
    }
    
    int Line::getLength( void ) {
       return *ptr;
    }
    
    void display(Line obj) {
       cout << "Length of line : " << obj.getLength() << endl;
    }
    
    // Main function for the program
    int main() {
    
       Line line1(10);
    
       Line line2 = line1; // This also calls copy constructor
    
       display(line1);
       display(line2);
    
       return 0;
    } 
    
    尝试一下
    编译并执行上述代码后,将产生以下结果-
    
    Normal constructor allocating ptr
    Copy constructor allocating ptr.
    Copy constructor allocating ptr.
    Length of line : 10
    Freeing memory!
    Copy constructor allocating ptr.
    Length of line : 10
    Freeing memory!
    Freeing memory!
    Freeing memory!