在什么情况下将“this"指针传递给类方法? [英] In what scenario 'this' pointer is passed to the class methods?

查看:32
本文介绍了在什么情况下将“this"指针传递给类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读this"指针,我想我比原来更了解它,但我仍然需要一些澄清.所以,据我所知,如果你有

I was doing some reading on the 'this' pointer, and I think I understand it more than I originally did, but I still need some clarification. So, by my understanding, if you have

class Simple
{
private:
    int m_nID;

public:
    Simple(int nID)
    {
        SetID(nID);
    }

    void SetID(int nID) { m_nID = nID; }
    int GetID() { return m_nID; }
};

SetID(int nID)函数实际上在语义上转换为:

The SetID(int nID) function actually is semantically converted into:

void SetID(Simple* const this, int nID) { this->m_nID = nID; }

有一个this 指针指向一个类的所有成员函数,在大多数情况下,这是有道理的.但是如果你有一个不带参数的成员函数会发生什么?是否有this"指针?如果是这样,它是否指向返回类型而不是参数类型?

It makes sense that, there is a this pointer for all member functions of a class, for the most part. But what happens if you have a member function that takes no arguments? Is there a 'this' pointer? If so, does it point to the return type instead of the argument type?

推荐答案

但是如果你有一个不接受任何成员函数会发生什么争论?是否有this"指针?如果是这样,它是否指向返回类型而不是参数类型?

But what happens if you have a member function that takes no arguments? Is there a 'this' pointer? If so, does it point to the return type instead of the argument type?

即使你的方法没有任何参数,它仍然有一个隐藏的参数,那就是this指针.

Even your method do not have any argument, it still have one hidden parameter, that is this pointer.

这篇关于在什么情况下将“this"指针传递给类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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