如何访问类成员在类中指向的地址

How do I access the address that a member of a class is pointing to while within the class?

本文关键字:地址 成员 访问 何访问      更新时间:2023-10-16

input.cpp 有错误的函数

void Input::isKeyPressed()
{
    if ( sf::Keyboard::isKeyPressed ( sf::Keyboard::S ) )
    {
        // Here's The Error
        *Input::playerOne.move(0.0 ,  1.0);
    }
}

更多详情

此函数是类 Input 的实现,该类有一个私有变量,用于指向程序的 int main() 中的 sf::RectangleShape 的指针。

我正在尝试访问 sf::RectangleShape 的实例化,以便在屏幕上向下移动对象。我不想创建一个全局变量类只是为了完成这项工作。我只是希望能够访问该特定对象的方法。

您需要指向成员运算符的指针

Input::playerOne->move(0.0, 1.0);

显式范围解析Input::不是必需的,可以重写为

playerOne->move(0.0, 1.0);