指针未正确返回值

pointer not returning the value correctly

我在程序中 return 指针的值有问题,指针的值没有被保存,当它读取时它是 returning null。

Header代码:

class PlayerHK : public Player {
public:
    PlayerHK();

    ULONG player_hp();
    ULONG player_power();
    ULONG player_hp2();
    ULONG player_power2();

private:

    struct CPlayer
    {
        BYTE padding[0x20];
        ULONG hp;
        ULONG power;
    };

    CPlayer *player;
};

主要代码:

PlayerHK::PlayerHK() {

        player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));

    }

    ULONG PlayerHK::player_hp() {
        return player->hp; //does not return the value
    }

    ULONG PlayerHK::player_power() {
        return player->power; //does not return the value
    }

    ULONG PlayerHK::player_hp2() {
        player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
        return player->hp; //returns the value
    }

    ULONG PlayerHK::player_power2() {
        player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
        return player->power; //returns the value
    }

当程序我运行读取PlayerHK时,值不应该被保存?我是不是忘了做点什么?

如果我理解正确,你问的是为什么

player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));

在构造函数中 运行 时将 player 设置为 NULL,但在 player_hp2 或 player_power2.

中 运行 时则不会

显而易见的答案是,这个内存位置 (0x00B1C4E5) 在构造对象时保存值 NULL,而在调用 player_hp2 或 player_power2 时保存不同的值。也许在构造函数 运行 时播放器尚未创建,因此指向播放器(您正在阅读的播放器)的指针为 NULL。