☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

用 static_cast 将成员指针转为基类成员指针时,基类中没有相关成员

C++-Undefined-Behavior-34
目录 › next › previous

示例:

struct B { int b; };
struct D: B { int d; };

int D::* mpb = &D::b;
int D::* mpd = &D::d;

static_cast<int B::*>(mpb);    // OK
static_cast<int B::*>(mpd);    // Undefined behavior

例中基类没有成员 d,将指向成员 d 的成员指针转为基类成员指针会导致未定义的行为。

依据

ISO/IEC 14882:2003 5.2.9(9)-undefined ISO/IEC 14882:2011 5.2.9(12)-undefined

规则

ID_downCast
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.