In C++, a derived class object can be assigned to base class, but the other way is not possible.

  1. class Base {
  2. int x,y;
  3. };
  4.  
  5. class Derived : public Base {
  6. int z, w;
  7. };
  8.  
  9. int main() {
  10. Derived d;
  11. Base b = d;
  12. }

Object slicing happens when a derived class object assigned to base class, additional attributes of derived class is sliced off to form base class.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Base {
  5. protected:
  6. int i;
  7. public:
  8. Base(int a) {
  9. i = a;
  10. }
  11. virtual void display() {
  12. cout << "I am Base class object, i = " << i << endl;
  13. }
  14. };
  15.  
  16. class Derived : public Base {
  17. int j;
  18. public:
  19. Derived(int a, int b) : Base(a) {
  20. j = b;
  21. }
  22. virtual void display() {
  23. cout << "I am Derived class object, i = " << i << ", j = " << j << endl;
  24. }
  25. };
  26.  
  27. void somefunc(Base obj) {
  28. obj.display();
  29. }
  30.  
  31. int main() {
  32. Base b(33);
  33. Derived d(45, 54);
  34. somefunc(b);
  35. somefunc(d);
  36. return 0;
  37. }

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

  1. void somefunc(Base *obj) {
  2. ...
  3. }
  4. void somefunc(Base &obj) {
  5. ...
  6. }

from http://www.geeksforgeeks.org/object-slicing-in-c/

Object Slicing in C++的更多相关文章

  1. 从零开始学C++之虚函数与多态(一):虚函数表指针、虚析构函数、object slicing与虚函数

    一.多态 多态性是面向对象程序设计的重要特征之一. 多态性是指发出同样的消息被不同类型的对象接收时有可能导致完全不同的行为. 多态的实现: 函数重载 运算符重载 模板 虚函数 (1).静态绑定与动态绑 ...

  2. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  3. C++ 虚函数在基类与派生类对象间的表现及其分析

    近来看了侯捷的<深入浅出MFC>,读到C++重要性质中的虚函数与多态那部分内容时,顿时有了疑惑.因为书中说了这么一句:使用“基类之指针”指向“派生类之对象”,由该指针只能调用基类所定义的函 ...

  4. C++ 常用术语(后续补充)

    内存对齐常量折叠 堆栈解退(stack unwinding) 模板特化模板偏特化 模板实例化 函数对象 单一定义规则(One-Definition Rule,ODR) 自引用   对象切片(objec ...

  5. 从零开始学C++之继承(二):继承与构造函数、派生类到基类的转换

    一.不能自动继承的成员函数 构造函数 析构函数 =运算符 二.继承与构造函数 基类的构造函数不被继承,派生类中需要声明自己的构造函数. 声明构造函数时,只需要对本类中新增成员进行初始化,对继承来的基类 ...

  6. Google C++ 代码规范

    Google C++ Style Guide   Table of Contents Header Files Self-contained Headers The #define Guard For ...

  7. boost bind及function的简单实现

    前面在做 http server 的时候,需要做一个回调的接口,要求能够绑定类的函数以及普通的函数到这个回调里,对于这种应用要求,选择 boost 的 bind 和 function 是最合适不过了, ...

  8. 面试总结之C/C++

    source code https://github.com/haotang923/interview/blob/master/interview%20summary%20of%20C%20and%2 ...

  9. 《深入浅出MFC》下载

    百度云及其他网盘下载地址:点我 编辑推荐 <深入浅出MFC>内含光盘一片,书中所有原始码与可执行文件尽在其中. 作者简介 侯俊杰,先生不知何许人也,闲静少言,不慕荣利.好读书,求甚解:每有 ...

随机推荐

  1. 利用Oracle 发送邮件(utl_smtp)

    发送邮件的方法有很多,.NET前台也可以通过创建邮件类的形式, 通过微软提供的System.Net.Mail.dll 也可以简单的发送邮件.但是代码比较长,操作起来虽然很简单(很多细节忽略了). 这里 ...

  2. ubuntu12.04(64位)下安装Adobe Flash Player

    2012-06-14 10:10:37   第一步,去adobe官方网站就可以,使用方便,打开网站:http://get.adobe.com/cn/flashplayer/根据自己的版本下载需要的.本 ...

  3. vmware-Binary translation is incompatible with long mode on this platform

    解决方法:Binary translation is incompatible with long mode on this platform. Disabling long mode. Withou ...

  4. 从P1到P7——我在淘宝这7年 - 子柳撰写

    http://kb.cnblogs.com/page/132752/来自博客园的整理版本,作者是子柳,博客地址:http://blog.sina.com.cn/calvinzhaoc (一) 2011 ...

  5. (一)Mina源代码解析之总体架构

    Apache Mina Server 是一个网络通信应用框架.也就是说,它主要是对基于TCP/IP.UDP/IP协议栈的通信框架(当然,也能够提供JAVA 对象的序列化服务.虚拟机管道通信服务等).M ...

  6. win10 配置 python3 + opencv3.2 + VideoCapture

    最近需要在 win10 上进行图片处理,使用深度学习框架 tensorflow ,所以安装了python3.5 + opencv3.2 + tensorflow + VideoCapture + PI ...

  7. 以源码编译的方式安装PHP与php-fpm

    首先是最基本的下载,解压,编译安装(以PHP 5.3.6 为例): wget http://www.php.net/get/php-5.3.6.tar.gz/from/this/mirrortar x ...

  8. Ribbon 和 wowza 的集成开发

            前言        Ribbon 是提供 REST 服务的区域感知负载均衡器,它在 wowza 的前端,应该部署在专业的 REST 容器下,而不是流媒体服务器 wowza 下. 本文介 ...

  9. atitit.高性能遍历 文本文件行 attilax总结

    atitit.高性能遍历 文本文件行 attilax总结 文件读写有以下几种常用的方法 1 通常io读取2.5s 1 nio读取或许越高的.. 2 NIO通常采用Reactor模式,AIO通常采用Pr ...

  10. 如何使用微信JS-SDK实际分享功能

    http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=7