C ++这个指针,在函数调用中隐藏参数 [英] C++ this pointer, hidden argument in function calls

查看:166
本文介绍了C ++这个指针,在函数调用中隐藏参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个堆栈类的成员和一个函数叫push。

I have a stack class with members and a function called push.

class STACK
{
    int data;
    public:
    void push(int x)
    {
        data=x;
    }
}

C ++如何转换此语句: p>

What does C++ do to convert this statement:

s1.push(3);

s1.push(this,3);

基本上我的问题是发生在生成 code>指针并将其作为隐藏参数传递?

Basically my question is what happens under the hood to generate the this pointer and pass it as a hidden argument?

我实际上是在C编码。我的目标是有一个程序,可能。对于这个我有功能指针作为结构的成员。因此,我想知道是否可能以某种方式跟踪哪个结构称为函数(像this指针)。我不想做像:s1.push(& s1,3);因为它击败了我的目的。

I am actually coding in C. My objective is to have a program which is as close to OOP as possible. For this i have function pointers as members of the structure. Therefore, I want to know if it is possible to somehow have track of which structure called the function (like a "this" pointer ). I don't want to do something like: s1.push(&s1,3); because it beats my purpose.

它可以转换s1.push(3);到s1.push(& 1,3);通过MACRO替换

推荐答案

语句:

s1.push(3);

未翻译为

 s1.push(this, 3);

但是类似这样:

STACK::push(&s1, 3); 

其中 STACK :: push 作为全局/命名空间或静态函数在类/命名空间 STACK 下,其原型将是:

Where STACK::push may be treated as global/namespace or static function under class/namespace STACK, whose prototype would be:

push(STACK const* pThis, int arg);

this 指针总是第一个参数到方法(函数)。如果方法有零参数,它仍然有一个参数(这个类的指针)。

The this pointer is always the first argument to the method (function). If method has zero argument, it will still have one argument (the this pointer of class).

这篇关于C ++这个指针,在函数调用中隐藏参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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