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 ...
随机推荐
- css中span元素的width属性无效果原因及多种解决方案
先运行下程序看下: <span style='width:300px;'>123</span> 输出:123 可以看到 span会自动根据包含的内容来变化宽度 这是因为:对于内 ...
- Mysql 优化,慢查询
最近项目上遇到点问题,服务器出现连接超时.上次也是超时,问题定位到mongodb上,那次我修改好了,这次发现应该不是这个的问题了. 初步怀疑是mysql这边出问题了,写的sql没经过压力测试,导致用户 ...
- Mac OS X下的移动光标和文字编辑快捷键
移动光标快捷键 Control-F 光标前进一个字符,相当于右键(F = Forward) Control-B 光标后退一个字符,相当于左键(B = Backward) Control-P 上移一行, ...
- call_user_func_array 应用场景分析
1. 场景一 a.你要调用的函数名是未知的 b.要调用函数的参数类型及个数也是未知的 定时任务类需要定时运行一个函数,则接口就可以类似这样设计: /** * 在$run_time时刻运行$call_b ...
- InstallShield卸载状态
uninstallaing() 它对应于-unistall 命令,在installshield 2009及之前的版本,在控制面板中选择"uninstall”会触发这个命令.但在install ...
- mongo批量更新、导入导出脚本
批量更新,一定要加上最后的条件: db.getCollection('cuishou_user').update( {,,,,,]}}, //query {$set:{)}},// update {m ...
- Python MQTT 最简单例程搭建
MQTT 不是普通的 client server 模型,他还加了一个 代理者. 根据剑锋的提示,先下载了 paho-mqtt 模块, ubuntu 14.04 上下载方法如下: sudo apt-ge ...
- Css+JS模拟实现可编辑的表格
表格在未编辑状态和编辑状态,需要定义两个不同的样式. 比如未编辑状态是lable的样式,两边有两个括号[],表示该表格可以编辑:编辑中的表格则表示成一个input框,可以输入. 基本思路就是,在表格中 ...
- vim 小技巧
cd - 返回刚才访问的目录shift+# 在vi中查找相同的关键字vi 中o在当前游标位置后插入一行 O在当前位置前插入一行0 $到行未 行首shift+s 删除一行,并且进入insert mode ...
- JQuery Notes
<script type="text/javascript" src="script.js"></script> $(document) ...