我想知道以下代码是否会导致未定义的行为:
#include <cstddef>
#include <cstdio>
struct IA {
virtual ~IA() {}
int a = 0;
};
struct IB {
virtual ~IB() {}
int b = 0;
};
struct C: IA, IB {};
int main() {
C* pc = nullptr;
IB* pib = pc;
std::printf("%p %p", (void*)pc, (void*)pib);
}
最佳答案
向上转换一个空指针被定义为给你另一个空指针:
4.10p3:
A prvalue of type "pointer to cv
D
", whereD
is a class type, can be converted to a prvalue of type "pointer to cvB
", whereB
is a base class ofD
. ... The null pointer value is converted to the null pointer value of the destination type.
关于c++ - 向上转换空指针是否会导致未定义的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29850716/