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++的更多相关文章

  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. 使用maven创建一个例子

    创建一个目录:D:\testmaven 在命令行中切换到D:\testmaven目录后输入: mvn archetype:generate 下载骨架,它会往本地工厂存信息 也可以直接使用带有参数的命令 ...

  2. ICSharpCode.SharpZipLib工具压缩与解压缩zip文件

    using System; using System.Collections.Generic; using System.IO; using System.Text; using ICSharpCod ...

  3. Mysql主从同步(1)-主从/主主环境部署梳理

    转 :https://www.cnblogs.com/kevingrace/p/6256603.html

  4. sudo配置文件/etc/sudoers格式

    sudo的配置文件 sudoers 一般在 /etc 目录下. 不过不管 sudoers 文件在哪儿,sudo 都提供了一个编辑该文件的命令:visudo 来对该文件进行修改. 讲解sudo配置文件/ ...

  5. 连接SQLServer的增删改查方法代码

    在Visual C++中用ADO进行数据库编程 1. 生成应用程序框架并初始化OLE/COM库环境 创建一个标准的MFC AppWizard(exe)应用程序CADOConnection,然后在使用A ...

  6. OGG目的端的checkpoint table被drop的修复方法

    OGG目的端的checkpoint table被drop的修复方法 參考自:OGG Replicat Failed Due To Check_point Table beingTruncated (文 ...

  7. CUGBACM Codeforces Tranning 3 题解

    链接:http://acm.hust.edu.cn/vjudge/contest/view.action? cid=62515#overview 描写叙述:第三场CF训练了.这次做的挺搞笑的,我记得这 ...

  8. C#多枚举值的写法与读法

    首先,定义枚举的时候必须是2,4,8,16这种2的次方的值. using System; using System.Collections.Generic; using System.Linq; us ...

  9. xa

    题目描述把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法.输入每个用例包含二个整数M和N.0<=m<=1 ...

  10. SpringBoot 整合 Security5

    https://my.oschina.net/yunduansing/blog/2032475 https://blog.csdn.net/SWPU_Lipan/article/details/805 ...