网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C++中为什么不要混用继承层级和数组

这篇文章主要讲解了“C++中为什么不要混用继承层级和数组”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++中为什么不要混用继承层级和数组”吧!

创新互联专注于湛河企业网站建设,响应式网站建设,商城网站建设。湛河网站建设公司,为湛河等地区提供建站服务。全流程定制网站开发,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

T.81:不要混用继承层级和数组

Reason(原因)

An array of derived classes can implicitly "decay" to a pointer to a base class with potential disastrous results.

派生类的数组可以隐式退化为可能带来灾难性后果的指向基类的指针。

Example(示例)

Assume that Apple and Pear are two kinds of Fruits.

假定Apple和Pear是两种Fruit。

void maul(Fruit* p)
{
   *p = Pear{};     // put a Pear into *p
   p[1] = Pear{};   // put a Pear into p[1]
}

Apple aa [] = { an_apple, another_apple };   // aa contains Apples (obviously!)

maul(aa);
Apple& a0 = &aa[0];   // a Pear?
Apple& a1 = &aa[1];   // a Pear?

Probably, aa[0] will be a Pear (without the use of a cast!). If sizeof(Apple) != sizeof(Pear) the access to aa[1] will not be aligned to the proper start of an object in the array. We have a type violation and possibly (probably) a memory corruption. Never write such code.

很有可能aa[0]是一个Pear(不需要类型转化)。如果sizeof(Apple)!=sizeof(Pear),对于aa[1]的访问位置就不会正确开始于下个对象。我们会遇到类型违反和可能的(几乎一定)内存破坏。永远不要这样写代码。

Note that maul() violates the a T* points to an individual object rule.

Alternative: Use a proper (templatized) container:

注意maul()已经违反了使用T*或onwer指明唯一对象原则。其他选项:使用适当的(模板化的)容器。

void maul2(Fruit* p)
{
   *p = Pear{};   // put a Pear into *p
}

vector va = { an_apple, another_apple };   // va contains Apples (obviously!)

maul2(va);       // error: cannot convert a vector to a Fruit*
maul2(&va[0]);   // you asked for it

Apple& a0 = &va[0];   // a Pear?

Note that the assignment in maul2() violated the no-slicing rule.

注意maul2()中的赋值操作违反了不要分割对象原则。

Enforcement(实施建议)

  • Detect this horror!

    检出这种可怕的问题!

感谢各位的阅读,以上就是“C++中为什么不要混用继承层级和数组”的内容了,经过本文的学习后,相信大家对C++中为什么不要混用继承层级和数组这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网页题目:C++中为什么不要混用继承层级和数组
URL标题:http://bjjierui.cn/article/ieppdc.html

其他资讯