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>内含光盘一片,书中所有原始码与可执行文件尽在其中. 作者简介 侯俊杰,先生不知何许人也,闲静少言,不慕荣利.好读书,求甚解:每有 ...
随机推荐
- iOS开发-应用之间的跳转及通信
Update 2016-08-12: 在Github的Demo上增加Mac自定义Url Scheme,可以在Safari上输入特定协议头打开应用,并传递参数) 简介 我们接下来将要实现应用程序之间的跳 ...
- Linux命令-帮助命令:whatis,apropos
whatis可以查看命令简化版的帮助内容 whatis ls 查看简化版的ls命令的帮助内容 whatis ifconfig 查看简化版的ifconfig命令的帮助内容 apropos可以查看配置文件 ...
- mysql误删root用户或者忘记root密码解决方法
解决方法一: 到其他安装了Mysql的服务器(前提是要知道该服务器上Mysql的root用户密 码),打开[Mysql的安装目录/var/mysql],将其中的user.frm.user.MYD.us ...
- Linux iptables常用命令
iptables 是 Linux 中重要的访问控制手段,是俗称的 Linux 防火墙系统的重要组成部分.这里记录了iptables 防火墙规则的一些常用的操作指令. 下面的操作以 CentOS 为基础 ...
- 8086汇编之 CALL 和 RET指令
Ret 和 call 也是转移指令,可是他们跟jmp不同的是,这两个转移指令都跟栈有关系. <1> ret 用栈中的数据改动IP的地址,从而实现近转移 ( ip ) = ( (ss)*16 ...
- 兼容浏览器的min-height和min-width
http://www.cnblogs.com/pigtail/archive/2012/06/28/2568646.html CSS 子元素宽度变宽时,如何撑开父元素https://zhidao.ba ...
- filebeat+kafka失败
filebeat端配置 #----------------------------- Kafka output -------------------------------- output.kafk ...
- 如何在iOS上实现对HTTPS的支持(转)
原文地址:http://blog.5ibc.net/p/101504.html 首先,需要明确你使用HTTP/HTTPS的用途,因为OSX和iOS平台提供了多种API,来支持不同的用途,官方文档< ...
- mysql学习笔记2--mysql的基本使用
4. 运行和关闭MySQL服务器 首先检查MySQL服务器正在运行与否.在资源管理器查看有没有mysqld的进程,如果MySQL正在运行,那么会看到列出来的 mysqld 进程.如果服务器没有运行,那 ...
- git学习(二):git config命令
不同的git config操作不同的参数文件 git config --global // 配置用户目录下的.gitconfig那文件 git config --system // 配置系统级配置文件 ...