gpt4 book ai didi

C++ 函数指针。它如何以及为何起作用?

转载 作者:行者123 更新时间:2023-11-28 06:46:03 24 4
gpt4 key购买 nike

考虑以下程序

#include <iostream>
#include <typeinfo>

template <class T>
void Output(const char * Str, T Func)
{
void *Ptr = reinterpret_cast<void *>(Func);
std::ptrdiff_t Num = reinterpret_cast<std::ptrdiff_t>(Ptr);
std::cout << Str << " " << Num << std::endl;
}

class TAnotherBase { long a[10]; };

struct TBase {
typedef void (TBase::*TFunc)();
TFunc Func;
TBase(TFunc F) {
Func = F;
Output("Ctor TBase ", Func);
}
void CallF() {
std::cout << "This in TBase: " << typeid(this).name() << " " << this << std::endl;
(this->*Func)();
}
};

struct TDerived: public TAnotherBase, public TBase {
TDerived(): TBase(static_cast<TBase::TFunc>(&TDerived::F)) {
Output("Ctor TDerived ", &TDerived::F);
CallF();
}
void F() {
std::cout << "This in TDerived::F: " <<typeid(this).name() << " " << this << std::endl;
}
};

int main(int argc, char **argv) {
TDerived Derived;
return 0;
}

它生成这个输出:

Ctor TBase  4197502                                (1)
Ctor TDerived 4197502 (2)
This in base: P5TBase 0x7fff6b30fc00 (3)
This in TDerived::F: P8TDerived 0x7fff6b30fbb0 (4)

这是怎么回事

我有函数 FTDerived类,然后我将指向函数的指针发送到 TBase类别:TDerived(): TBase(static_cast<TBase::TFunc>(&TDerived::F)) { (1) 输出函数指针。然后我在TDerived中输出函数指针类 (2) 并制作 TBase调用函数的类:`CallF(); (4), (5).

TAnotherBase是为了与众不同this TBase 的指针和 TDerived类。

那么,第一个问题。

我读到函数指针更像是相对于 this 的偏移量.如果是这样,为什么我在 (1) 和 (2) 中得到相同的函数指针值 - 4197502?

第二个问题

我输出 thisCallF函数(3)就可以了。但后来我调用函数 (this->*Func)();通过this (这是 TBase )和功能 F this神奇地变得完全不同this (4)!它改变了它的类型和值(value)!怎么可能?编译器如何仍然记得 Func (类型为 typedef void (TBase::*TFunc)(); )实际上来自 TDerived类(class)?编译器如何知道它应该调整 this在将其发送到 F 之前?为什么以及如何运作?

最佳答案

Itanium ABI 中描述了一种可以完成此操作的示例- 如果您对如何实现 C++ 类模型感兴趣,我强烈建议您阅读整篇文档。

当您从指向派生成员函数的指针转换为指向基成员函数的指针时,编译器知道当您调用它时 this 将指向未派生的基。所以它需要确保它做的第一件事是将 this 从 base* 修改回 derived*。它可以通过发明一个修改 this 并跳转到实际函数的 shim 函数来做到这一点,或者通过仅将偏移量与指针一起存储并在调用站点使用它(这是引用以上介绍)。或者我确定还有其他方法。

(static_cast 不仅仅是一个编译时操作;通常它必须在运行时做真正的工作。)

关于C++ 函数指针。它如何以及为何起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968510/

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