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 ...
随机推荐
- 命令行todo神器taskwarrior使用简介
简介 taskwarrior是一个命令行的任务管理神器,同时也有服务端,支持同步. 语法规则为 安装 Linux上可以直接软件包管理器安装 Window可以用cygwin Mac可以用homebrew ...
- tongjiword,write / read file demo
1.tong ji letter demo mport sys def tongjiword(): fi =open(paht,'r') cont=fi.read() wl={} for line i ...
- Android ViewPage的使用(一)
ViewPage是一个简单的页面切换组件,左右滑动的话,有效果,和ListView一样 也需要配合适配器(PagerAdapter)来使用. 先来个效果图吧 先随便创建3个view页面,用于放到 Vi ...
- mac 系统中vim安装ctags插件
1,mac自带的ctags程序不是exuberant ctags, 所以使用时会出现问题,所以要重新安装一个: brew install exuberant ctags 安装完, which ctag ...
- MySQL日志——二进制日志
Mac怎么这么坑呢,搞了2小时了.唉 先来一个简单的,挖好坑,明天解决. 终端进入mysql: mysql> set global general_log=on; 然后进行数据库的任意操作: 查 ...
- Visual Studio的NuGet包管理器无法加载
由于网络原因,虽然地址http://www.nuget.org和https://www.nuget.org/api/v2/在浏览器可以正常打开,但是在VS中使用默认的NuGet程序包源经常加载不出来, ...
- [转]nginx负载均衡的五种算法
1.round robin(默认) 轮询方式,依次将请求分配到各个后台服务器中,默认的负载均衡方式. 适用于后台机器性能一致的情况. 挂掉的机器可以自动从服务列表中剔除. 2.weight 根据权重来 ...
- python多线程同步机制condition
#!/usr/bin/env python# -*- coding: utf-8 -*- import threadingimport time def customer(cond): t = thr ...
- windows下安装和配置多个版本的JDK
https://jingyan.baidu.com/article/47a29f2474ba55c015239957.html 如何在windows下安装和配置多个版本的jdk,本文将带你在windo ...
- css3动画属性系列之transform细讲scale缩放
下面我们从3个方面开始介绍: 1.scale(x,y) 对元素进行缩放 X表示水平方向缩放的倍数 | Y表示垂直方向的缩放倍数 Y是一个可选参数,没有设置的话,则表示X,Y两个方向的缩放倍数是一样的. ...