gpt4 book ai didi

c++ - 指向成员表达式 UB 的指针

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:47 24 4
gpt4 key购买 nike

标准N3797::5.5/4:

Abbreviating pm-expression.*cast-expression as E1.*E2, E1 is called the object expression. If the dynamic type of E1 does not contain the member to which E2 refers, the behavior is undefined

您能否提供此处描述的生成 UB 的示例?

最佳答案

这意味着 E1 在运行时的实际类型没有成员 E2。当 E1 是实际具有成员 E2 的类型的基类的实例时,会发生一个简单的情况。

struct A
{
};

struct B : public A
{
int b;
};

int main()
{
A* a = new A();
B* b = static_cast<B*>(a);
int B::* pbm = &B::b;
(*b).*pbm = 6;
}

关于c++ - 指向成员表达式 UB 的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26072899/

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