拷贝构造函数和赋值函数的区别
来源:互联网 发布:大疆 知乎 编辑:程序博客网 时间:2024/06/11 01:05
// 测试代码可直接运行
// ConstructorTest.h 文件
{
public:
ConstructorTest();
~ConstructorTest();
ConstructorTest(char *pName);
ConstructorTest(const ConstructorTest &otherObj); //A objA = ObjB; // 调用的是拷贝构造函数(无论类还是结构体).
ConstructorTest& operator =(const ConstructorTest &otherObj); // A objC; objC = objA;// 调用的是赋值函数(无论类还是结构体).
void UseConstructorTest(ConstructorTest testObj); // 函数参数中传递对象值或者函数返回对象值时,调用的都是拷贝构造函数。
ConstructorTest GetConstructorTest(ConstructorTest &testObj);
void PrintContent();
private:
char* m_pName;
};
// ConstructorTest.cpp文件
#include <string.h>
#include <iostream>
using namespace std;
#include "classCharacter.h"
struct tagName
{
char* m_pName;
tagName()
{
m_pName = NULL;
}
tagName& operator =(const tagName& b)
{
if(m_pName != NULL)
{
delete m_pName;
}
m_pName = new char[strlen(b.m_pName) + 1];
strcpy(m_pName, b.m_pName);
cout<<"tagName operator = call !"<<endl;
return (*this);
}
tagName(const tagName &b)
{
m_pName = new char[strlen(b.m_pName) + 1];
strcpy(m_pName, b.m_pName);
cout<<"tagName Deep Constructor call !"<<endl;
}
};
ConstructorTest::ConstructorTest()
{
m_pName = NULL;
}
ConstructorTest::~ConstructorTest()
{
if(m_pName != NULL)
{
delete []m_pName;
}
}
ConstructorTest::ConstructorTest(char *pName)
{
cout<<"ConstructorTest Constructor Call!"<<endl;
m_pName = new char[strlen(pName) + 1];
strcpy(m_pName, pName);
}
ConstructorTest::ConstructorTest(const ConstructorTest &otherObj)
{
/*if(m_pName != NULL)
{
delete [] m_pName; // 声明的时候赋值才会调用,故不用清理内存;如果清理会dump机
}*/
cout<<"ConstructorTest Deep Constructor Call!"<<endl;
m_pName = new char[strlen(otherObj.m_pName) + 1];
strcpy(m_pName, otherObj.m_pName);
}
ConstructorTest& ConstructorTest::operator =(const ConstructorTest &otherObj)
{
cout<<"ConstructorTest Assigment Constructor Call!"<<endl;
if(this == &otherObj)// 检查自赋值
{
return (*this);
}
if(m_pName != NULL)
{
delete [] m_pName;
}
m_pName = new char[strlen(otherObj.m_pName) + 1];
strcpy(m_pName, otherObj.m_pName);
return (*this);
}
void ConstructorTest::UseConstructorTest(ConstructorTest testObj)
{
ConstructorTest *pTestObj = &testObj;
pTestObj->PrintContent();
}
ConstructorTest ConstructorTest::GetConstructorTest(ConstructorTest &testObj)
{
return testObj;
}
void ConstructorTest::PrintContent()
{
cout<<"The content is: "<<m_pName<<endl;
}
void main()
{
cout<<"-------------Class Contructor Test-------------"<<endl;
ConstructorTest Name1("Sandy Ou");
Name1.PrintContent();
ConstructorTest Name2 = Name1;
Name2.PrintContent();
ConstructorTest Name3;
Name3 = Name1;
Name3.PrintContent();
Name3.UseConstructorTest(Name2);
ConstructorTest Name4 = Name3.GetConstructorTest(Name2);
Name4.PrintContent();
cout<<"-------------struct Contructor Test-------------"<<endl;
tagName nameA;
nameA.m_pName = "JesseCen";
tagName nameB = nameA;
cout<<"tagName content is:"<<nameB.m_pName<<endl;
while(1);
}
- 拷贝构造函数和赋值构造函数的区别
- 拷贝构造函数和赋值构造函数的区别
- 赋值构造函数和拷贝构造函数的区别
- 赋值构造函数和拷贝构造函数的区别
- 拷贝构造函数和赋值构造函数的区别
- 区别拷贝构造函数和赋值函数
- 拷贝构造函数和赋值函数的区别
- 拷贝构造函数和赋值函数的区别
- 拷贝构造函数和赋值操作符的区别
- 拷贝构造函数和赋值表达式的区别
- 拷贝构造函数和赋值操作符的区别 .
- 拷贝构造函数和赋值表达式的区别
- 使用拷贝构造函数和赋值运算符的区别
- 拷贝构造函数和赋值表达式的区别
- C++中构造函数,拷贝构造函数和赋值函数的区别和实现
- 拷贝构造函数与赋值构造函数的区别
- 拷贝构造函数与赋值构造函数的区别
- 拷贝构造函数和拷贝赋值函数
- supervise程序进行程序管理监控
- 互联网广告的盈利模式
- linux 的进程调度原理
- Python3的变化
- ubuntu14.04 下 mysql 存储目录迁移
- 拷贝构造函数和赋值函数的区别
- JavaScript中delete操作符不能删除的对象
- hdu 5015 233 Matrix(矩阵快速幂)
- 20. 枚举数和迭代器
- Mysql 左右连接查询理解
- linux--管道特性
- 将H264与AAC打包Ipad可播放的TS流的总结
- uva 507
- 进程与线程的联系与区别