C++继承细节 -2
- 继承与动态内存分配
//基类定义
class BaseClass {
private:
char *label;
public:
BaseClass() {}
BaseClass(const char *l);
virtual ~BaseClass();
BaseClass(const BaseClass &bc);
BaseClass &operator=(const BaseClass &bc);
};
BaseClass::BaseClass(const BaseClass &bc)
{
this->label = new char[std::strlen(bc.label)+1];
std::strcpy(this->label, bc.label);
}
BaseClass &BaseClass::operator=(const BaseClass &bc)
{
this->label = new char[std::strlen(bc.label)+1];
std::strcpy(this->label, bc.label);
}
BaseClass::~BaseClass()
{
delete[] this->label;
}
1. 派生类中的数据成员没用`new`分配内存,则不需要为派生类提供 *复制构造函数*、*赋值运算符*;因为在使用已知对象对另一个对象初始化时派生类的默认复制函数将调用基类的显示复制函数(BaseClass(const BaseClass &bc))进行深拷贝,同理赋值运算符也一样。
//派生类定义
class DerivedClass : public BaseClass {
private:
char style[20]; //使用栈空间
public:
DerivedClass() {}
DerivedClass(const char *st);
};
2. 派生类中的数据成员使用`new`分配内存,则派生类需要提供 *复制构造函数*、 *赋值运算符*,具体实现如下:
//派生类
class DerivedClass : public BaseClass {
private:
char *style;
public:
DerivedClass() {}
DerivedClass(const char *st);
~DerivedClass() {delete[] this->style;}
DerivedClass(const DerivedClass &dc);
DerivedClass &operator=(const DerivedClas &dc);
};
//显示复制构造函数
DerivedClass::DerivedClass(const DerivedClass &dc) : BaseClass(dc)
{
this->style = new char[std::strlen(dc.style)+1];
std::strcpy(this->style, dc.style);
}
//赋值运算符
DerivedClass & DerivedClass::operator=(const DerivedClass &dc)
{
if (this == &dc) {
return *this;
}
delete[] this->style;
//注意,注意,注意
BaseClass::operator=(dc); //显示调用基类赋值运算符函数
this->style = new char[std::strlen(dc.style)+1];
std::strcpy(this->style, dc.style);
return *this;
}
C++继承细节 -2的更多相关文章
- python继承细节
不要子类化内置类型 内置类型(由C语言编写)不会调用用户定义的类覆盖的特殊方法. 例如,子类化dict作为测验: class DoppeDict(dict): def __setitem__(self ...
- C# 继承细节
假定没有为类定义任何显式的构造函数,这样编译器就会为所有的类提供默认的构造函数,在后台会进行许多操作,编译器可以很好地解决层次结构中的所有问题,每个类中的每个字段都会初始化为默认值.但在添加了一个我们 ...
- C++继承细节 -1
为什么基类析构函数最好要使用 virtual 进行修饰? class A { private: ...... public: ~A(); A() {} }; class B : public A { ...
- JAVA_SE基础——39.继承
在面向对象程序设计中,可以从已有的类派生出新类. 这称做继承(inheritance). 白话解释: 例子1:继承一般是指晚辈从父辈那里继承财产,也可以说是子女拥有父母给予他们的东西. 例子2:猫和狗 ...
- JavaScript类继承
和其他功能一样,ECMAScript 实现继承的方式不止一种.这是因为 JavaScript 中的继承机制并不是明确规定的,而是通过模仿实现的.这意味着所有的继承细节并非完全由解释程序处理.作为开发者 ...
- JavaScript中继承机制的模仿实现
首先,我们用一个经典例子来简单阐述一下ECMAScript中的继承机制. 在几何学上,实质上几何形状只有两种,即椭圆形(是圆形的)和多边形(具有一定数量的边).圆是椭圆的一种,它只有一个焦点.三角形. ...
- ECMAScript 继承机制实现
继承机制的实现 要用 ECMAScript 实现继承机制,您可以从要继承的基类入手.所有开发者定义的类都可作为基类.出于安全原因,本地类和宿主类不能作为基类,这样可以防止公用访问编译过的浏览器级的代码 ...
- 一文看懂JS继承
继承是OOP中大家最喜欢谈论的内容之一,一般来说,继承都两种方式:接口继承和实现继承而JavaScript中没有接口继承需要的方法,因此只能依靠实现继承.在讲继承的实现之前,我们首先来回顾一下什么是继 ...
- JS如何实现继承?
JS的继承是基于JS类的基础上的一种代码复用机制.换言之,有了代码,我们就不需要复制之前写好的方法,只要通过简捷的方式 复用之前自己写的或同事写的代码.比如一个弹出层,我们需要在上面做一些修改.同事写 ...
随机推荐
- Web.py报错:OSError: No socket could be created -- (('0.0.0.0', 8080):
web.py报错 Python代码: import web urls = ( '/(.*)', 'hello' ) app = web.application(urls, globals()) cla ...
- 调用Linux的busybox,通过linux命令来获取AndRoidIP
//根据busybox获取本地Mac public static String getLocalMacAddressFromBusybox(){ String result = "" ...
- Ubuntu安装使用pyltp和StanfordCoreNLP
环境:Ubuntu 16.04+anaconda3 一.pyltp 1. 安装 直接用pip安装: pip install pyltp 然后下载语言模型库,网址:https://pan.baidu.c ...
- linux定时备份MySQL数据库并删除七天前的备份文件
1.创建备份文件夹 #cd /bak#mkdir mysqldata 2.编写运行脚本 #nano -w /usr/sbin/bakmysql.sh 注:如使用nano编辑此代码需在每行尾添加’&am ...
- 【Leetcode】【Medium】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 一道算法题-从1到n整数中1出现的次数
1. 题目描述 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,1一共出现了5次. 2. 题目来源 第一次看到是在 ...
- TortoiseGit基本操作
使用之前需要下载git for windows,tortoisegit . 1.TortoiseGit使用与操作 1.1克隆 打开一个要存放项目的文件夹下,右键Git Clone...进行克隆 弹出克 ...
- February 21 2017 Week 8 Tuesday
To make each day count. 让每一天都物有所值. We always want to make our life meaningful, however, the acutal f ...
- angularJS报错$apply already in progress的原因和解决方法
如果我们使用了AngularJS中的$scope.$apply()或者$scope.$digest(),我们很可能会遇到类似下面的错误,虽然这个错误没有太大影响,但是在日志中看起来还是很不爽的,日志中 ...
- 用js或JQuery模拟点击a标签的操作
一.用js模拟点击a标签的操作. jsp代码: <a id="login" href="${pageContext.request.contextPath}/log ...