gpt4 book ai didi

c++ - __thiscall 关键字的用途是什么?

转载 作者:行者123 更新时间:2023-12-04 01:03:28 32 4
gpt4 key购买 nike

阅读 calling conventions 时有一个目的很明确的 thiscall 约定。

我知道 thiscall 定义了一个属于类的函数,因为这些函数需要一个对象作为非静态函数的第一个隐藏参数。

所以如果我没理解错的话,下面的函数应该是thiscall。不需要使用关键字,因为编译器在那里没有太多选择,必须这样声明。

 class Foo
{
public:
int function(void) { return 1; }
};

现在当我像这样在类外声明函数时:

int __thiscall Otherfunction(void) { return 1; }
int __thiscall Otherfunction(Foo *t) { return 1; }

然后我得到编译器错误:

error C3865: '__thiscall': can only be used on native member functions

它甚至是有道理的,因为您将如何调用这些函数?那么为什么不能使用这个关键字甚至存在呢?或者是否有一些可以/必须使用它的用例?

最佳答案

these functions require an object as their first hidden parameter for non-static functions

具有讽刺意味的是,你自己回答了一半,因为你在这里描述的是 fastcall,而不是 thiscall,这是一种调用成员函数的古老替代方法。 thiscallthis 存储在寄存器中,而不是堆栈中。

这就是你的答案,不是因为 thiscall 可以用在非成员函数上,而是因为你可以在成员函数上使用不同的约定(fastcall 当然还有 nakedcall 或者随便叫什么)。

关于c++ - __thiscall 关键字的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67321629/

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