我需要知道在调用C++中的类方法时,隐式“this”指针是第一个参数还是最后一个参数。即:是先将其插入堆栈还是最后将其插入堆栈。

换句话说,我要问的是,编译器是否采用了被调用的类方法:

int foo::bar(foo *const this, int arg1, int arg2);
//or:
int foo::bar(int arg1, int arg2, foo *const this);

因此,通过扩展,更重要的是,这还将回答G++是将this指针分别推到最后还是推到第一个。我审问了google,但没有发现太多。

另外,调用C++函数时,它们是否与C函数具有相同的作用? IE:
push ebp
mov ebp, esp

总而言之:被调用的类方法看起来像这样吗?
; About to call foo::bar.
push dword 0xDEADBEEF
push dword 0x2BADBABE
push dword 0x2454ABCD ; This one is the this ptr for the example.
; this code example would match up if the this ptr is the first argument.
call _ZN3foo3barEpjj

谢谢,非常有义务。

编辑:澄清一下,我正在使用GCC/G++ 4.3

最佳答案

这取决于编译器的调用约定和目标体系结构。

默认情况下,Visual C++不会将其压入堆栈。对于x86,编译器将默认使用“thiscall”调用约定,并将其传递给ecx寄存器。如果您为成员函数指定__stdcall,它将作为第一个参数被压入堆栈。

对于VC++上的x64,前四个参数在寄存器中传递。这是第一个参数,并传递到rcx寄存器中。

雷蒙德·陈(Raymond Chen)几年前在电话 session 上有一系列报道。这是x86x64文章。

关于c++ - C++隐含了这个,以及确切地将其插入堆栈的方式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1297205/

10-12 21:48