加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

切片意味着什么?

发布时间:2020-12-16 05:38:43 所属栏目:百科 来源:网络整理
导读:在C FAQ站点中提到 – “通过值作为基类对象传递时,较大的派生类对象被分割”,切片是什么意思?任何样品要展示? http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html#faq-31.8 我正在使用VSTS 2008 native C作为我的开发环境. 解决方法 报
在C FAQ站点中提到 – “通过值作为基类对象传递时,较大的派生类对象被分割”,切片是什么意思?任何样品要展示?

http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html#faq-31.8

我正在使用VSTS 2008 native C作为我的开发环境.

解决方法

报价 this lecture:

Slicing

Suppose that class D is derived from
class C. We can think of D as class C
with some extra data and methods. In
terms of data,D has all the data that
C has,and possible more. In terms of
methods,D cannot hide any methods of
C,and may have additional methods. In
terms of existing methods of C,the
only thing that D can do is to
override them with its own versions.

If x is an object of class D,then we
can slice x with respect to C,by
throwing away all of the extensions
that made x a D,and keeping only the
C part. The result of the slicing is
always an object of class C.

07001

Design Principle: Slicing an object
with respect to a parent class C
should still produce a well-formed
object of class C.

Usage Warning: Even though D is-a C,
you must be careful. If you have a
argument type that is a C and you
supply a D it will be sliced if you
are doing call by value,pointer,or
reference. See the example below.

Note on virtual functions. Their
signatures are used to identify which
one to execute.

Watch out for the sliced = operator,it can make the lhs inconsistent. Also,the operator= is never virtual,it wouldn’t make sense. For example,suppose classes A,B are both subclasses of class C. Just because an A is a C,and a B is a C,it doesn’t mean you can assign a B object to an A object. Without run-time type information you cannot make a safe assignment.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读