//头文件kaobei.h
class CExample
{
public:
CExample(){pBuffer=NULL;nSize=0;} //构造函数
~CExample(){delete [] pBuffer;} //析构函数
CExample(const CExample&); //拷贝构造函数
void Init(int n){pBuffer=new char[n];nSize=n;}
private:
char *pBuffer; //类的对象中包含指针,指向动态分配的内存资源
int nSize;
};
CExample::CExample(const CExample& RightSides) //拷贝构造函数的定义
{
nSize=RightSides.nSize; //复制常规成员
pBuffer=new char[nSize]; //复制指针指向的内容
memcpy(pBuffer,RightSides.pBuffer,nSize*sizeof(char));
}
//.cpp文件
#include "iostream"
#include "kaobei.h"
using namespace std;
int main(int argc,char* argv[])
{
CExample theObjone;
theObjone.Init(40);
CExample theObjtwo=theObjone;
}
拷贝构造函数
最新推荐文章于 2025-01-20 23:43:53 发布