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 ...
随机推荐
- java二维码的生成与解析代码
二维码,是一种采用黑白相间的平面几何图形通过相应的编码算法来记录文字.图片.网址等信息的条码图片.如下图 二维码的特点: 1. 高密度编码,信息容量大 可容纳多达1850个大写字母或2710个数字或 ...
- jsp----标签编程(JSTL)
标签编程简介 JSP的开发是在HTML代码中嵌入了大量的Java代码,但是这样一来会使得JSP页面中充满了Java程序,修改或维护起来非常的不方便, 定义一个简单的标签----空标签 要想实现一个标签 ...
- 【Unity】JsonUtility解析集合(collections)类型(List)
Unity自带的Json解析工具类JsonUtility居然没有API用于解析集合类型,也太鬼扯了吧. https://stackoverflow.com/questions/36239705/ser ...
- gSoap的“error LNK2001: 无法解析的外部符号 _namespaces”解决方法
错误 2 error LNK2001: 无法解析的外部符号 _namespaces 解决方法: 1. 在工程中定义 WITH_NONAMESPACES 宏 2.尝试 "#include &q ...
- Postgres快速创建大量测试数据
DROP TABLE IF EXISTS color; CREATE TABLE color( cname ), remark ) ); ),) ,); 插入10000000数据之后,用时51秒 对于 ...
- 机器学习--详解人脸对齐算法SDM-LBF
引自:http://blog.csdn.net/taily_duan/article/details/54584040 人脸对齐之SDM(Supervised Descent Method) 人脸对齐 ...
- MVC源码学习之AuthorizeAttribute
常见的Controller定义方式: public class HomeController : Controller { public ActionResult Index() { return V ...
- [数据结构]迪杰斯特拉(Dijkstra)算法
基本思想 通过Dijkstra计算图G中的最短路径时,需要指定起点vs(即从顶点vs开始计算). 此外,引进两个集合S和U.S的作用是记录已求出最短路径的顶点,而U则是记录还未求出最短路径的顶点(以及 ...
- 多选下拉框带搜索(aps.net)
自己写了一个带搜索功能的多选下拉框,为了要获取值,就没有封装插件,实现思路 1.一个文本框 做搜索 2.一个文本框显示选中文本,一个隐藏控件存值 3.一个div里面绑定CheckBoxList控件(这 ...
- Repeater 嵌套,子级Repeater获取 父级Repeater 中的值
第一种方法,子级Repeater中绑定父级的某个字段: <%# DataBinder.Eval((Container.NamingContainer.NamingContainer as Rep ...