gpt4 book ai didi

c++ - 通过基类函数指针调用派生类

转载 作者:可可西里 更新时间:2023-11-01 17:55:50 27 4
gpt4 key购买 nike

我能否通过基类函数指针调用派生类,如下例所示?

我知道我的示例有效,但它是否保证始终如此(假设对象实际实现了该功能!),或者这只是我正在使用的编译器的特性?

按照这种逻辑,难道不能简单地从“CBase”(在本例中是空的,所以我猜没有开销)派生所有类并忽略函数指针中的类型吗?

#include <iostream>

struct CBase
{
};

struct CDerived : CBase
{
void MyFunction()
{
std::cout << "Called OK" << std::endl;
}
};

typedef void (CBase::*FunctionPointer)();


int main()
{
CDerived* base = new CDerived();

FunctionPointer pointer = static_cast<FunctionPointer>(&CDerived::MyFunction);
(base->*pointer)();

delete base;
}

示例使用场景:一种派生类,它采用一个或多个指向基类中“回调”的指针。是否可以仅使用派生类定义回调类型,从而放弃对模板的需要?

最佳答案

是的,它保证有效。来自 [expr.static.cast]:

A prvalue of type “pointer to member of D of type cv1 T” can be converted to a prvalue of type “pointer to member of B of type cv2 T”, where B is a base class (Clause 10) of D, if cv2 is the same cv-qualification as, or greater cv-qualification than, cv1.70 If no valid standard conversion from “pointer to member of B of type T” to “pointer to member of D of type T” exists (4.11), the program is ill-formed. The null member pointer value (4.11) is converted to the null member pointer value of the destination type. If class B contains the original member, or is a base or derived class of the class containing the original member, the resulting pointer to member points to the original member.

在这种情况下,我们将指向 void() 类型的 CDerived 成员的指针转换为指向 CBase 成员的指针ov 类型 void()CBase 是包含原始成员的类的基类,因此生成的指针指向原始成员。

来自 [expr.mptr.oper]:

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.

在这种情况下,pointer 指向原始成员。 base 有那个成员。所以这很好。


请注意,在您的示例中,base 实际上是一个 CDerived*。写成同样有效:

CDerived d;
CBase* b = &d;

(b->*pointer)(); // ok - the dynamic type of b contains the member to which pointer refers

关于c++ - 通过基类函数指针调用派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37662100/

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