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 ...
随机推荐
- Laravel 5.4 Cache::put的一个小坑
使用的是Cache的file驱动,然而在\Cache::put($key,$value)时发现,并没有存入. 一开始以为是file驱动的问题,后来跟踪代码发现,居然源码里当过期时间不设置时,根本不保存 ...
- Go语言学习(四)经常使用类型介绍
1.布尔类型 var v1 bool v1 = true; v2 := (1==2) // v2也会被推导为bool类型 2.整型 类 型 长度(字节) 值 范 围 int8 1 128 ~ 12 ...
- HTTP1.1 协议
Response Headers 响应头 Expires 设置响应内容的过期时间 过期时间头信息属性值只能是HTTP格式的日期时间,HTTP的日期时间必须是格林威治时 间(GMT),而不是本地时间.举 ...
- IoCopyCurrentIrpStackLocationToNext与IoSetCompletionRoutine的深入理解
1.IoCopyCurrentIrpStackLocationToNext是拷贝本层的IO_STACK_LOCATION 到下一层.在楚狂人的驱动教程中说:如果对irp完成之后的事情有兴趣,并打算在完 ...
- Java扫描classpath指定包路径下所有class
在写框架时 经常需要扫描classpath指定包路径下带有某个Annotation的类,自己整理了一下 封装成一个工具类了,供大家参考. 源代码ClassPathResourceScanner.jav ...
- PCL中分割方法的介绍(2)
(2)关于上一篇博文中提到的欧几里德分割法称之为标准的距离分离,当然接下来介绍其他的与之相关的延伸出来的聚类的方法,我称之为条件欧几里德聚类法,(是我的个人理解),这个条件的设置是可以由我们自定义的, ...
- PDF文件分割和合并
今天自己用C#实现了下PDF文件的分割和合并,大家可以试用一下. 代码和使用说明在这里:https://github.com/cserspring/pdf_split_merge 有什么意见,大家可以 ...
- 标签<view>文字自动换行
<view style='word-break:break-all;'>{{con.blog}}</view>
- JQ 点击指定文本框显示div。点击其他区域隐藏DIV
<input id="username" type="text" style="width:90%;margin-top: 40px;" ...
- 《FPGA全程进阶---实战演练》第九章 计数器要注意
本小节我们来做一个好玩的事情,就是计数器,还记得在做LED自加实验时我们就曾经提到过关于计数器的相关议题,那么这节我们就来讨论讨论. 探讨一下如下的问题:请用verilog记八个数的写法,分析这个可以 ...