Object Slicing in C++
In C++, a derived class object can be assigned to base class, but the other way is not possible.
class Base {
int x,y;
};
class Derived : public Base {
int z, w;
};
int main() {
Derived d;
Base b = d;
}
Object slicing happens when a derived class object assigned to base class, additional attributes of derived class is sliced off to form base class.
#include <iostream>
using namespace std; class Base {
protected:
int i;
public:
Base(int a) {
i = a;
}
virtual void display() {
cout << "I am Base class object, i = " << i << endl;
}
}; class Derived : public Base {
int j;
public:
Derived(int a, int b) : Base(a) {
j = b;
}
virtual void display() {
cout << "I am Derived class object, i = " << i << ", j = " << j << endl;
}
}; void somefunc(Base obj) {
obj.display();
} int main() {
Base b(33);
Derived d(45, 54);
somefunc(b);
somefunc(d);
return 0;
}
Output
I am Base class object, i = 33
I am Derived class object, i = 45
We can avoid such unexpected behavior with the use of pointer or reference. Object slicing won't happen if we set the function arguments to pointer or reference because all pointer or reference have same amount memory
we can avoid object slicing by writing like this
void somefunc(Base *obj) {
...
}
void somefunc(Base &obj) {
...
}
from http://www.geeksforgeeks.org/object-slicing-in-c/
Object Slicing in C++的更多相关文章
- 从零开始学C++之虚函数与多态(一):虚函数表指针、虚析构函数、object slicing与虚函数
一.多态 多态性是面向对象程序设计的重要特征之一. 多态性是指发出同样的消息被不同类型的对象接收时有可能导致完全不同的行为. 多态的实现: 函数重载 运算符重载 模板 虚函数 (1).静态绑定与动态绑 ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- C++ 虚函数在基类与派生类对象间的表现及其分析
近来看了侯捷的<深入浅出MFC>,读到C++重要性质中的虚函数与多态那部分内容时,顿时有了疑惑.因为书中说了这么一句:使用“基类之指针”指向“派生类之对象”,由该指针只能调用基类所定义的函 ...
- C++ 常用术语(后续补充)
内存对齐常量折叠 堆栈解退(stack unwinding) 模板特化模板偏特化 模板实例化 函数对象 单一定义规则(One-Definition Rule,ODR) 自引用 对象切片(objec ...
- 从零开始学C++之继承(二):继承与构造函数、派生类到基类的转换
一.不能自动继承的成员函数 构造函数 析构函数 =运算符 二.继承与构造函数 基类的构造函数不被继承,派生类中需要声明自己的构造函数. 声明构造函数时,只需要对本类中新增成员进行初始化,对继承来的基类 ...
- Google C++ 代码规范
Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard For ...
- boost bind及function的简单实现
前面在做 http server 的时候,需要做一个回调的接口,要求能够绑定类的函数以及普通的函数到这个回调里,对于这种应用要求,选择 boost 的 bind 和 function 是最合适不过了, ...
- 面试总结之C/C++
source code https://github.com/haotang923/interview/blob/master/interview%20summary%20of%20C%20and%2 ...
- 《深入浅出MFC》下载
百度云及其他网盘下载地址:点我 编辑推荐 <深入浅出MFC>内含光盘一片,书中所有原始码与可执行文件尽在其中. 作者简介 侯俊杰,先生不知何许人也,闲静少言,不慕荣利.好读书,求甚解:每有 ...
随机推荐
- UVA1492 - Adding New Machine(扫描线)
UVA1492 - Adding New Machine(扫描线) option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- 公司交换机arp 绑定操作
1.首先登入:192.168.1.1 2.sys 3.dis arp | inc 192.168.1.49(查看该ip绑定情况) 4.undo arp 192.168.1.49(不绑定命令)
- eval函数处理JSON数据需要加括号
在将服务器端构建好的JSON数据转化为可用的JavaScript对象时常常使用eval函数.如下: var dataJson = eval('(' + data + ')'); 在转化的时候需要将JS ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- sms_queue 短信队列
git地址:https://github.com/Filix/sms_queue 简介 通过队列的方式发送短信,暂时实现了redis作为队列. 以实现的第三方短信服务: 百悟.漫道. 发送短信方,只需 ...
- Oracle常用单行函数(原创)
前言: 想把单行函数进行一个比较全面的总结,并分享给有需要的人,有不明之处还请多多指教. SQL函数:Oracle的内置函数,包括了单行函数和多行函数,本文重点讲解单行函数.单行函数又可以分为许多类, ...
- mosquitto ---SSL/TLS 单向认证+双向认证
生成证书 # * Redistributions in binary form must reproduce the above copyright # notice, this list of ...
- 非super user管理会话
在gp中取消或者中断某个用户的超长时间或者SQL存在问题的会话.假设无法拥有超级用户将无法运行该类操作. 首先我们创建两个用户t1.t2,而且使用t1登录到数据库. [gpadmin@wx60 ~ ...
- linux kill 关闭进程命令
杀死进程最安全的方法是单纯使用kill命令,不加修饰符,不带标志. 首先使用ps -ef命令确定要杀死进程的PID,然后输入以下命令: # kill -pid 注释:标准的kill命令通常都能达到目的 ...
- beyond compare比较工具设置
beyond compare用于比较的工具,云盘:比较 链接: https://pan.baidu.com/s/1boZbB0F