C++ 隐式参数的顺序:this 和返回的对象,哪个先? [英] the order of C++ implicit arguments: this and the returned object, which goes first?

查看:26
本文介绍了C++ 隐式参数的顺序:this 和返回的对象,哪个先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 中,成员函数最多可以有 2 个隐式参数:this 指针和返回对象的地址.它们在显式参数之前,但是:哪个先?

In C++, a member function may have up to 2 implicit arguments: the this pointer and the address of the returned object. They precede the explicit arguments, but: which one goes first?

特别是,我对 Android NDK(基于 gcc,ARM)中发生的事情很感兴趣.

In particular, I'm interested in what happens in Android NDK (gcc-based, ARM).

示例:

class MyClass {
public:
  int a,b;
  MyClass(int aa,int bb):a(aa),b(bb){};
  MyClass modif(int da, int db) {return MyClass(a+da,b+db);} //an object is returned
};

推荐答案

您似乎是在询问调用约定(在跳转到函数之前注册/堆栈参数和返回值的存储位置).

It seems like you're asking about the calling convention (which registers / where on the stack arguments & return values are stored before branching to a function).

在很大程度上取决于目标 ABI,即使如此,它也相当复杂.对于 ARMv5/6/7,过程调用标准 [pdf] 是你的圣经.

It depends a lot on the target ABI and even then it's fairly complicated. For ARMv5/6/7, the Procedure call standard [pdf] is your bible.

如果你真的很在意的话,你真的应该阅读整篇文章,但这里是你感兴趣的部分:

You should really read the whole thing if you actually care, but here are the parts you're interested in:

  • 在 r0 中返回不大于 4 个字节的复合类型.
  • 大于 4 字节的复合类型,或其大小不能由调用者和被调用者静态确定,存储在内存中的地址处,该地址在调用函数时作为额外参数传递.
    • 如果子程序是一个在内存中返回结果的函数,则将结果的地址放在在 r0 中,NCRN 设置为 r1.

    还有

    • 对于 C++,隐式 this 参数作为额外参数传递,紧接在第一个用户参数之前.
    • For C++, an implicit this parameter is passed as an extra argument that immediately precedes the first user argument.

    所以在大多数情况下,返回的复合值的地址在 $r0 中,而隐含的 this 指针在 $r1 中.

    So in most cases the address of a returned composite value is in $r0 and the implicit this pointer is in $r1.

    这篇关于C++ 隐式参数的顺序:this 和返回的对象,哪个先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆