什么是对象切片? [英] What is object slicing?

查看:130
本文介绍了什么是对象切片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在IRC中提到它,但google没有很好的答案。

Someone mentioned it in the IRC, but google doesn't have a good answer.

推荐答案

切片您将派生类的对象分配给基类的实例,从而丢失部分信息 - 其中一些被切分。

"Slicing" is where you assign an object of a derived class to an instance of a base class, thereby losing part of the information - some of it is "sliced" away.

例如,

class A {
   int foo;
};

class B : public A {
   int bar;
};

因此, B 类型的对象有两个数据成员 foo bar

So an object of type B has two data members, foo and bar.

你写的是:

B b;

A a = b;

然后 b code> a

Then the information in b about member bar is lost in a.

这篇关于什么是对象切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆