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. Git使用教程(全)

    Git是什么? Git是目前世界上最先进的开源的分布式版本控制系统(没有之一),用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开 ...

  2. javascript通用函数库

    /* -------------- 函数检索 -------------- trim函数:                         trim() lTrim() rTrim() 校验字符串是否 ...

  3. Linux下connect超时处理

    1.前言 最近在写一个测试工具,要求快速的高效率的扫描出各个服务器开放了哪些端口.当时想了一下,ping只能检测ip,判断服务器的网络是连通的,而不能判断是否开放了端口.我们知道端口属于网络的传输层, ...

  4. Ubuntu下安装java

    1.首先到java.com下载最新版本的jdk.下面是jdk8的网址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...

  5. 1. A + B Problem【easy】

    Write a function that add two numbers A and B. You should not use + or any arithmetic operators. Not ...

  6. shell判断文件夹是否存在

    #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...

  7. 成为 Team Leader 后我最关心的那些事

    成为 Team Leader 后我最关心的那些事   推荐序 老有人问我 iOS 开发如何提高,今天收到一个来自网易的朋友投稿,分享他在成为 iOS 项目负责人之后面临的问题.文章中分享的如何招人,如 ...

  8. python学习笔记(2)--sublimeText3运行python

    https://www.zhihu.com/question/22904994 知乎用户 To the knowledge 74 人赞同 如果是想在sublime里要python shell那种交互或 ...

  9. 一款纯css3实现的tab选项卡

    今天给大家带来一款纯css3实现的tab选项卡.单击左侧的选项的时候,右侧内容以动画的形式展示.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class=&quo ...

  10. Android WebView的注意事项

    1.修改权限,添加<uses-permission android:name="android.permission.INTERNET"/> 2.loadUrl方法要写 ...