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>内含光盘一片,书中所有原始码与可执行文件尽在其中. 作者简介 侯俊杰,先生不知何许人也,闲静少言,不慕荣利.好读书,求甚解:每有 ...
随机推荐
- FlatBuffers与protobuf性能比較
FlatBuffers发布时.顺便也发布了它的性能数据,详细数据请见Benchmark. 它的測试用例由下面数据构成"a set of about 10 objects containing ...
- C# 通过form表单下载文本文件
public void DownLoadConfigFile(string name) { //获取文件字符串内容 var data = _service.ReadFileStr(_configure ...
- 深入理解JavaScript系列(转载)
深入理解JavaScript系列 深入理解JavaScript系列(1):编写高质量JavaScript代码的基本要点 深入理解JavaScript系列(2):揭秘命名函数表达式 深入理解JavaSc ...
- centos增加软连接
#增加软连接 ln -s /usr/local/git/bin/* /usr/bin/
- 微软的SQLHelper类(含完整中文注释)
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
- 网页字体生成工具fontello firefox下无效,未跨域,研究两天得出解决办法
fontello是一个非常好的web font生成工具,但是在使用过程中发现生成的字体在firefox下死活渲染不出来,只有chrome可以正常渲染,字体文件和页面在同域下. 试过各种办法,最后发现一 ...
- 数据库连接 UnicodeEncodeError: 'latin-1' codec can't encode character u'\u4eac' in position 0: ordinal not in range(256),UnicodeEncodeError: 'latin-1' codec can't encode character
处理方法就是,设置连接和游标的charset为你所希望的编码,如utf8, 连接数据库的时候加上 charset="utf8 db = MySQLdb.connect(host= '14 ...
- AutoFac文档3(转载)
目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 服务类型,名称和键 同 ...
- mybatis-spring 项目简介
mybatis-spring 主要是为了解决 spring3.0 中支持 mybatis3.0 1 mybatis-spring 主要包含以下几个部分 数据源 SqlSessionFactory Sq ...
- SpringCloud-服务注册与发现
这里我们会用到Spring Cloud Netflix,该项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的N ...