在 C++ 中是否有等同于 java 的 'classname.this'?

is there an equivalent to 'classname.this' from java in c++?

我正在尝试从虚函数调用私有派生 class 方法,但我不知道如何从虚函数调用派生 class 的函数。

这是Game class in Game.h

中的虚函数声明
virtual void Screen::KeyListener::onChar(char c);

当我在不同的 .cpp 文件中定义此函数时,我不知道如何访问游戏中声明的任何方法 class。我只能从屏幕调用函数 class.

我试过这样的东西

Game::makeMove(1);

(其中make move是Game中定义的私有成员函数class)

但这只是给我错误 "a nonstatic member reference must be relative to a specific object."

抱歉,如果我的 wording/terminology 有点不对,我对 C++ 还是很陌生。我的大部分经验都是 java。

您可以像下面的代码一样使用静态:

Static Member Functions

These functions work for the class as whole rather than for a particular object of a class.

It can be called using an object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.

示例:

class X
{
 public:
 static void f(){};
};

int main()
{
 X::f();   // calling member function directly with class name
}

这些函数不能访问普通数据成员和成员函数,只能访问静态数据成员和静态成员函数。

它没有任何 "this" 关键字,这就是它无法访问普通会员的原因。

您确定该定义实际上是在 class 游戏而不是 class 屏幕中定义函数 onChar 吗?如果您不小心将其写为 onChar 的定义(我可以想象这很容易发生):

void Screen::KeyListener::onChar(char c) {}

那么你在class屏幕中定义函数。