How to use base class's assignment operator in C++
看了android下的代码,好长时间没看了,有个关于C++的知识点不是很清楚,查了下:
如何使用基类中的赋值运算符?
引述自http://stackoverflow.com/questions/1226634/how-to-use-base-classs-constructors-and-assignment-operator-in-c
参考:《C++ Primer》4ed_CN p_495
编译器的默认设置:
struct base
{
base() { std::cout << "base()" << std::endl; }
base( base const & ) { std::cout << "base(base const &)" << std::endl; }
base& operator=( base const & ) { std::cout << "base::=" << std::endl; }
};
struct derived : public base
{
// compiler will generate:
// derived() : base() {}
// derived( derived const & d ) : base( d ) {}
// derived& operator=( derived const & rhs ) {
// base::operator=( rhs );
// return *this;
// }
};
int main()
{
derived d1; // will printout base()
derived d2 = d1; // will printout base(base const &)
d2 = d1; // will printout base::=
}
如果我们在派生类中自己定义了拷贝构造函数、"="重载运算符,那么我们就需要显示的调用基类中的拷贝构造函数和基类中的“=”重载运算符。
class Base
{
/* .. */
}; class Derive: public Base
{
//如果没有显式调用Base(d),则调用Base中的无参的Base拷贝构造函数
Derived(const Derived &d): Base(d)
{
/* ... */
} Derived &Derived::operator=(const Derived &rhs)
{
if(this != &rhs)
{
Base::operator=(rhs);
/* ... */
} return *this;
}
};
How to use base class's assignment operator in C++的更多相关文章
- Default Assignment Operator and References
We have discussed assignment operator overloading for dynamically allocated resources here . This is ...
- Lintcode208 Assignment Operator Overloading (C++ Only) solution 题解
[题目描述] Implement an assignment operator overloading method. Make sure that: The new data can be copi ...
- Effective C++ 第0章 copy constructor和copy assignment operator
拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...
- copy constructor和copy assignment operator的区别
拷贝构造函数(copy constructor)被用来以一个对象来初始化同类型的另一个对象,拷贝赋值运算符(copy assignment operator)被用来将一个对象中的值拷贝到同类型的另一个 ...
- When should we write our own assignment operator in C++?
The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need t ...
- Copy constructor vs assignment operator in C++
Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...
- PythonStudy——赋值运算符 Assignment operator
eg: num = 10 num += 1 # 等价于 num = num + 1 => 11 print(num) 特殊操作: 1.链式赋值 a = b = num print(a, b, n ...
- <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<二>
<Item 9> Never call virtual functions during construction or destruction 1.you shouldn't call ...
- <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<一>
<Item 5> Know what functions C++ silently writes and calls 1.If you don't declare them yoursel ...
随机推荐
- 可以用的远程maven地址
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- Web程序中的懒加载异常说明及解决方案
所谓懒加载(lazy)就是延时加载,延迟加载. 什么时候用懒加载呢,我只能回答要用懒加载的时候就用懒加载. 至于为什么要用懒加载呢,就是当我们要访问的数据量过大时,明显用缓存不太合适, 因为内存容量有 ...
- [转]tppabs是什么?如何去除tppabs?
原文地址:http://www.cnblogs.com/gdsblog/archive/2017/03/25/6616561.html 不得不说,一款伟大的软件,就是用来解放人类双手的,Telepor ...
- APMServ5.2.6 升级php5.2 到 5.3版本及Memcache升级!
一.如何选择PHP5.3的VC9版本和VC6版本 VC6是什么?VC6就是legacy Visual Studio 6 compiler,就是使用这个编译器编译的.VC9是什么?VC9就是the V ...
- C语言 · 比较字符串
算法训练 比较字符串 时间限制:1.0s 内存限制:512.0MB 编程实现两个字符串s1和s2的字典序比较.(保证每一个字符串不是另一个的前缀,且长度在100以内).若s1和s2相 ...
- Spring Boot war包&jar包对比
使用 Maven对SpringBoot程序进行打包处理有两种格式:一种是war包,一种是jar包. 虽然我们将springboot应用打包成了war包,但是我们依然可以使用 java -jar的方式来 ...
- 微信小程序——倒计时功能
做小程序项目中,需要做一个倒计时功能,如下图: 记录一下实现步骤: 1.考虑到这个功能可能会有多处用到,所以把倒计时的函数写在utils.js里面: const formatNumber = n =& ...
- [spark] spark 特性、简介、下载
[简介] 官网:http://spark.apache.org/ 推荐学习博客:http://dblab.xmu.edu.cn/blog/spark/ spark是一个采用Scala语言进行开发,更快 ...
- QVariant类及QVariant与自定义数据类型转换的方法
这个类型相当于是Java里面的Object,它把绝大多数Qt提供的数据类型都封装起来,起到一个数据类型“擦除”的作用.比如我们的 table单元格可以是string,也可以是int,也可以是一个颜色值 ...
- 关于Unity中的刚体和碰撞器的相关用法(二)
在关于Unity中的刚体和碰撞器的相关用法(一)的基础上 有一个plane平面,一个ball球体,都挂了碰撞器,ball挂了刚体Rigidbody,写了一个脚本ball挂载在球体上,球体从空中落下装机 ...