gpt4 book ai didi

c++ - 为什么多态性不适用于 C++ 中的数组?

转载 作者:IT老高 更新时间:2023-10-28 22:16:39 24 4
gpt4 key购买 nike

#include <iostream>

using namespace std;

struct Base
{
virtual ~Base()
{
cout << "~Base(): " << b << endl;
}

int b = 1;
};

struct Derived : Base
{
~Derived() override
{
cout << "~Derived(): " << d << endl;
}

int d = 2;
};

int main()
{
Base* p = new Derived[4];
delete[] p;
}

输出如下:(Visual Studio 2015 with Clang 3.8)

~Base(): 1
~Base(): 2
~Base(): -2071674928
~Base(): 1

为什么多态性不适用于 C++ 中的数组?

最佳答案

给定,

Base* p = Derived[4];

C++11 标准制定

delete [] p;

是未定义的行为。

5.3.5 Delete

...

2 ... In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.

从内存布局的角度来看,delete [] p; 的原因也是有道理的。将导致未定义的行为。

如果 sizeof(Derived)N , new Derived[4]分配的内存类似于:

+--------+--------+--------+--------+
| N | N | N | N |
+--------+--------+--------+--------+

一般来说,sizeof(Base) <= sizeof(Derived) .在您的情况下,sizeof(Base) < sizeof(Derived)自从 Derived有一个额外的成员变量。

使用时:

Base* p = new Derived[4];

你有:

p
|
V
+--------+--------+--------+--------+
| N | N | N | N |
+--------+--------+--------+--------+

p+1指向自 sizeof(Base) < sizeof(Derived) 以来第一个对象中间的某个位置.

       p+1
|
V
+--------+--------+--------+--------+
| N | N | N | N |
+--------+--------+--------+--------+

当在 p+1 上调用析构函数时,指针不指向对象的开始。因此,该程序表现出未定义行为的症状。


相关问题

由于 Base 的大小不同和 Derived ,您不能使用 p 遍历动态分配数组的元素.

for ( int i = 0; i < 4; ++i )
{
// Do something with p[i]
// will not work since p+i does not necessary point to an object
// boundary.
}

关于c++ - 为什么多态性不适用于 C++ 中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com