在这个C嵌套类内部,如何通过一个长尾来替代this关键字?
- 内容介绍
- 文章标签
- 相关推荐
本文共计230个文字,预计阅读时间需要1分钟。
若你有以下嵌套类结构:
cppclass foo {public: class bar { public: int barMethod() const; protected: int barVar; };protected: int fooVar;};
在.cpp文件中,你可以通过以下方式实现`barMethod`:
cppint foo::bar::barMethod() const { // 实现你的方法}
如果我有以下嵌套类: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的指针.
本文共计230个文字,预计阅读时间需要1分钟。
若你有以下嵌套类结构:
cppclass foo {public: class bar { public: int barMethod() const; protected: int barVar; };protected: int fooVar;};
在.cpp文件中,你可以通过以下方式实现`barMethod`:
cppint foo::bar::barMethod() const { // 实现你的方法}
如果我有以下嵌套类: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的指针.

