C++ 拷贝构造函数
c++ 拷贝构造函数
什么是拷贝构造函数?首先对于普通类型的对象来说,它们之间的复制是很简单的,例如:
int a = 100; int b = a;
而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量。
下面看一个类对象拷贝的简单例子。
#include <iostream>
using namespace std;
class cexample {
private:
int a;
public:
//构造函数
cexample(int b)
{ a = b;}
//一般函数
void show ()
{
cout<<a<<endl;
}
};
int main()
{
cexample a(100);
cexample b = a; //注意这里的对象初始化要调用拷贝构造函数,而非赋值
b.show ();
return 0;
}
运行程序结果为:
100
从以上代码的运行结果可以看出,系统为对象 b 分配了内存并完成了与对象 a 的复制过程。就类对象而言,相同类型的类对象是通过拷贝构造函数来完成整个复制过程的。
拷贝构造函数是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象。拷贝构造函数通常用于:
-
通过使用另一个同类型的对象来初始化新创建的对象。
-
复制对象把它作为参数传递给函数。
-
复制对象,并从函数返回这个对象。
如果在类中没有定义拷贝构造函数,编译器会自行定义一个。如果类带有指针变量,并有动态内存分配,则它必须有一个拷贝构造函数。拷贝构造函数的最常见形式如下:
classname (const classname &obj) {
// 构造函数的主体
}
在这里,obj 是一个对象引用,该对象是用于初始化另一个对象的。
#include <iostream>
using namespace std;
class line
{
public:
int getlength( void );
line( int len ); // 简单的构造函数
line( const line &obj); // 拷贝构造函数
~line(); // 析构函数
private:
int *ptr;
};
// 成员函数定义,包括构造函数
line::line(int len)
{
cout << "normal constructor allocating ptr" << endl;
// 为指针分配内存
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;
}
// 程序的主函数
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 ); // 简单的构造函数
line( const line &obj); // 拷贝构造函数
~line(); // 析构函数
private:
int *ptr;
};
// 成员函数定义,包括构造函数
line::line(int len)
{
cout << "normal constructor allocating ptr" << endl;
// 为指针分配内存
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;
}
// 程序的主函数
int main( )
{
line line1(10);
line line2 = line1; // 这里也调用了拷贝构造函数
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!

c++ 类和对象
