C++拷贝构造函数的继承
2016-11-05 22:17
155 查看
拷贝构造函数要求把所有变量都需要做拷贝。在有继承关系情况先,子类的拷贝构造函数,需要调用父类拷贝构造函数。示例代码如下:
class Base{
public:
virtual ~Base();
Base(const char *pStr);
Base(const Base &other);
virtual void CallFunction() ;
public:
char *m_pBase;
};
Base::Base(const char *pStr){
if (pStr) {
long iLen = strlen(pStr)+1;
m_pBase = new char[iLen];
memset(m_pBase, 0, iLen);
strcpy(m_pBase, pStr);
}}
Base::~Base(){
if (m_pBase) {
delete [] m_pBase;
m_pBase = NULL;
}
}
Base::Base(const Base &other){
if (m_pBase) {
delete m_pBase;
m_pBase = NULL;
}
long iLen = strlen(other.m_pBase)+1;
m_pBase = new char[iLen];
memset(m_pBase, 0, iLen);
strcpy(m_pBase, other.m_pBase);
}
class Child:public Base{
public:
~Child();
Child(const char *pStr , const char *pBase);
Child(const Child &other);
public:
char *m_pChild;
};
Child::Child(const char *pStr , const char*pBase):Base(pBase){//初始化列表中调父类构造函数
if (pStr) {
long iLen = strlen(pStr)+1;
m_pChild = new char[iLen];
memset(m_pChild, 0, iLen);
strcpy(m_pChild, pStr);
}
}
Child::Child(const Child&other):Base(other){//调父类拷贝构造函数
if (m_pChild) {
delete m_pChild;
m_pChild = NULL;
}
long iLen = strlen(other.m_pChild)+1;
m_pChild = new char[iLen];
memset(m_pChild, 0, iLen);
strcpy(m_pChild, other.m_pChild);
}
Child::~Child(){
if (m_pChild) {
delete [] m_pChild;
m_pChild = NULL;
}
}
Test:
Child ch("child","base");
Child ch2(ch);
文章来源
http://www.cnblogs.com/zhidao-chen/p/3831320.html
class Base{
public:
virtual ~Base();
Base(const char *pStr);
Base(const Base &other);
virtual void CallFunction() ;
public:
char *m_pBase;
};
Base::Base(const char *pStr){
if (pStr) {
long iLen = strlen(pStr)+1;
m_pBase = new char[iLen];
memset(m_pBase, 0, iLen);
strcpy(m_pBase, pStr);
}}
Base::~Base(){
if (m_pBase) {
delete [] m_pBase;
m_pBase = NULL;
}
}
Base::Base(const Base &other){
if (m_pBase) {
delete m_pBase;
m_pBase = NULL;
}
long iLen = strlen(other.m_pBase)+1;
m_pBase = new char[iLen];
memset(m_pBase, 0, iLen);
strcpy(m_pBase, other.m_pBase);
}
class Child:public Base{
public:
~Child();
Child(const char *pStr , const char *pBase);
Child(const Child &other);
public:
char *m_pChild;
};
Child::Child(const char *pStr , const char*pBase):Base(pBase){//初始化列表中调父类构造函数
if (pStr) {
long iLen = strlen(pStr)+1;
m_pChild = new char[iLen];
memset(m_pChild, 0, iLen);
strcpy(m_pChild, pStr);
}
}
Child::Child(const Child&other):Base(other){//调父类拷贝构造函数
if (m_pChild) {
delete m_pChild;
m_pChild = NULL;
}
long iLen = strlen(other.m_pChild)+1;
m_pChild = new char[iLen];
memset(m_pChild, 0, iLen);
strcpy(m_pChild, other.m_pChild);
}
Child::~Child(){
if (m_pChild) {
delete [] m_pChild;
m_pChild = NULL;
}
}
Test:
Child ch("child","base");
Child ch2(ch);
文章来源
http://www.cnblogs.com/zhidao-chen/p/3831320.html
相关文章推荐
- c++ 拷贝构造函数 继承
- C++组合和继承 组合中会涉及到默认构造函数和拷贝构造函数的问题
- c++ 拷贝构造函数 继承
- C++ 继承 构造函数、拷贝构造函数
- C++ 拷贝构造函数和赋值构造函数
- C++程序设计——继承
- [置顶] 从零开始学C++之虚继承和虚函数对C++对象内存模型造成的影响(类/对象的大小)
- C++中的拷贝构造函数和拷贝赋值操作符+const成员变量初始化(5)---《Effective C++》
- C++拷贝构造函数(深拷贝、浅拷贝)
- 以c++的角度看OC中类成员和继承属性
- C++转换与继承
- c++继承中的内存布局
- 在C++中子类继承和调用父类的构造函数方法
- C++转向C#的疑惑:难道C#中没有拷贝构造函数 ?(转载)
- C++第10/11周项目 继承与派生 【日期时间类 】(复习新版)
- C++ 不给力之不可继承
- C++中虚函数工作原理和(虚)继承类的内存占用大小计算
- struct使用继承c++代码实例及运行结果
- C++中的多重继承与虚继承的问题
- C++继承与派生