c++ 拷贝构造函数 继承
拷贝构造函数要求把所有变量都需要做拷贝。在有继承关系情况先,子类的拷贝构造函数,需要调用父类拷贝构造函数。示例代码如下:
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)+;
m_pBase = new char[iLen];
memset(m_pBase, , 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)+;
m_pBase = new char[iLen];
memset(m_pBase, , 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)+;
m_pChild = new char[iLen];
memset(m_pChild, , 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)+;
m_pChild = new char[iLen];
memset(m_pChild, , 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);
c++ 拷贝构造函数 继承的更多相关文章
- C++构造函数 & 拷贝构造函数 & 派生类的构造函数 & 虚继承的构造函数
构造函数 ,是一种特殊的方法 .主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 .特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数 ...
- OOP3(继承中的类作用域/构造函数与拷贝控制/继承与容器)
当存在继承关系时,派生类的作用域嵌套在其基类的作用域之内.如果一个名字在派生类的作用域内无法正确解析,则编译器将继续在外层的基类作用域中寻找该名字的定义 在编译时进行名字查找: 一个对象.引用或指针的 ...
- C++类中的一些细节(重载、重写、覆盖、隐藏,构造函数、析构函数、拷贝构造函数、赋值函数在继承时的一些问题)
1 函数的重载.重写(重定义).函数覆盖及隐藏 其实函数重载与函数重写.函数覆盖和函数隐藏不是一个层面上的概念.前者是同一个类内,或者同一个函数作用域内,同名不同参数列表的函数之间的关系.而后三者是基 ...
- c++ 构造函数,拷贝构造函数,析构函数与赋值操作符
题目: 为下面的Rectangle类实现构造函数,拷贝构造函数,赋值操作符,析构函数. class Shape { int no; }; class Point { int x; int y; }; ...
- 【C++对象模型】构造函数语意学之二 拷贝构造函数
关于默认拷贝构造函数,有一点和默认构造函数类似,就是编译器只有在[需要的时候]才去合成默认的拷贝构造函数. 在什么时候才是[需要的时候]呢? 也就是类不展现[bitwise copy semantic ...
- javascript 封装 构造函数继承 非构造函数继承
1 封装 把"属性"(property)和"方法"(method),封装成一个对象,甚至要从原型对象生成一个实例对象 1.1 简单封装:var cat1 = { ...
- 如何避免被C++默认拷贝构造函数忽悠?
一.背景介绍 因为工作关系,需要用到C++编程.对于我来说,虽然一直从事的是linux平台下的嵌入式软件开发,但深入用到C++的特性的地方并不多.对于C++,用得最多的无非是指针. ...
- 拷贝构造函数,深拷贝,大约delete和default相关业务,explicit,给定初始类,构造函数和析构函数,成员函数和内联函数,关于记忆储存,默认参数,静态功能和正常功能,const功能,朋友
1.拷贝构造 //拷贝构造的规则,有两种方式实现初始化. //1.一个是通过在后面:a(x),b(y)的方式实现初始化. //2.另外一种初始化的方式是直接在构造方法里面实现初始化. 案比例如以 ...
- C++拷贝构造函数总结
C++拷贝构造函数总结 目录: 拷贝构造函数的基础知识 拷贝构造函数的使用 拷贝构造函数的行为 1.拷贝构造函数的基础知识 拷贝构造函数(copy constructor)是构造函数,是拷贝已经存在的 ...
随机推荐
- 服务器端使用DeferredResult异步推送技术
注意1: org.springframework.web.context.request.async.DeferredResult; 是Spring4.0以后的版本才有的,注意Spring版本,然后添 ...
- 剑指offer--11.数组中出现次数超过一半的数字
unique(), count()函数好用 ---------------------------------------------------------------------- 时间限制:1秒 ...
- linux C gbk utf-8编码转换
http://blog.csdn.net/sealyao/article/details/5043138
- vbox sethduuid
laozha@abc$ /usr/bin/vboxmanage internalcommands sethduuid /ud1/VBOX/centos7/CentOS7.vdi UUID change ...
- hdu 3932 Groundhog Build Home——模拟退火
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3932 注意平均值与最远的点距离为0的情况.所以初值设成-1,这样 id 就不会乱.不过设成0也可以.注意判 ...
- dockerfile http_php
FROM centos6.6-php5.5:0.0.1 MAINTAINER syberos:wangmo RUN mv /etc/php.ini /etc/php.ini.bak COPY ./ph ...
- 解决docker 下来镜像出现 error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net的问题
http://f2d6cb40.m.daocloud.io [root@node2 ~]# docker --version ...
- debug时打到了URLClassLoader.class里面,
一.解决方法,查看breakpoints,看有没有在这个类里面打断点,有时会系统自动打断电在这个类里面, 二.在设置里面,找到debug,去掉debug的前面几个断电设置.
- java代码I/O流类
package com.aini; //流类rr //流操作的步骤: /*1.找到指定File 2.实例化字节流.InputStream/OutputStream/Reader/Writer 3.读/ ...
- Redis value的5种类型及常见操作
Redis本身存储就是一个hash表,实际实࣫比hash表更复一些,后续讲存储结构时会细讲Key只有String类型Value包括String ,Set,List,Hash,Zset五中类型 STRI ...