gpt4 book ai didi

c++ - 具有继承内存存储的对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:59 24 4
gpt4 key购买 nike

假设我有一些类似这个例子的类。

class A {
int k, m;
public:
A(int a, int b) {
k = a;
m = b;
}
};

class B {
int k, m;
public:
B() {
k = 2;
m = 3;
}
};

class C : private A, private B {
int k, m;
public:
C(int a, int b) : A(a, b) {
k = b;
m = a;
}
};

现在,在 C 类对象中,变量是否以特定方式存储?我知道 POD 对象中会发生什么,但这不是 POD 对象...

最佳答案

在第 10 章派生类的介绍中,C++ 标准提到:

The order in which the base class subobjects are allocated in the most derived object (1.8) is unspecified.

因此,在您的示例中,C 对象每个都有一个类型为 A 的基类子对象和一个类型为 B 的基类子对象,但是A 基成员是在 B 基成员之前还是之后未指定。

关于c++ - 具有继承内存存储的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4837480/

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