加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C嵌套类中的“this”关键字

发布时间:2020-12-16 09:20:40 所属栏目:百科 来源:网络整理
导读:如果我有以下嵌套类: class foo {public: class bar { public: int barMethod() const; protected: int barVar; };protected: int fooVar;}; 然后在.cpp中,我可以用这种方式实现barMethod()吗? int foo::bar::barMethod() const { return this-fooVar + th
如果我有以下嵌套类:

class foo {
public:
    class bar {
    public:
        int barMethod() const;
    protected:
        int barVar;
    };

protected:
    int fooVar;
};

然后在.cpp中,我可以用这种方式实现barMethod()吗?

int foo::bar::barMethod() const {
    return this->fooVar + this->barVar;
}

我的意思是:
嵌套类中的这个关键字是指层次结构中上游的所有类吗?

解决方法

does this keyword in a nested class refer to all classes that are upstream in hierarchy?

不,只有“当前”类.类嵌套主要是词法.与Java相比,内部类可以与封闭外部类的实例相关联,foo :: bar与任何其他未嵌套的类非常相似.

如果要将bar实例与foo实例相关联,则需要在bar中捕获引用或指向foo的指针.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读