简体   繁体   English

(C ++)指向不同类中对象的指针的指针

[英](C++) Pointers to Pointers of objects in different classes

everyone. 大家。 I'm working on a final project for school and it's coming along great, but I've run into a bit of a problem with trying to use a pointer to a pointer. 我正在为学校做最后的项目,进展很好,但是尝试使用指向指针的指针遇到了一些问题。 I'll do my best to explain the problem below: 我将尽力解释以下问题:

So I have a class called Player that sort of looks like this: 所以我有一个叫做Player的类,看起来像这样:

class Player
{
    Player();
    int health;
    void adjustHealth(int);
};

Player::Player()
{
    health = 40;
}

void Player::adjustHealth(int adjust)
{
    health += adjust;
}

I have another class called Shelter, that include "Player.h" and looks a little like this: 我还有一个名为Shelter的类,其中包括“ Player.h”,看起来有点像这样:

class Shelter
{
    Shelter();
    Player* player;     // Create a pointer to Player class.
};

In the Shelter header file, I have the following in my default constructor: 在Shelter头文件中,默认构造函数中包含以下内容:

Shelter::Shelter()
{
    ...Other code here.
    player = new Player();
}

In the Shelter header file, I use this new player for things like: 在Shelter头文件中,我使用此新播放器执行以下操作:

 player->adjustHealth(-1);  // Subtract one health from the player.

Which works great. 哪个很棒。

The problem I'm facing is with creating another class called Church, that is in a separate header file and acts as a separate location in the game. 我面临的问题是创建另一个名为Church的类,该类位于单独的头文件中,并在游戏中充当单独的位置。 I want Church to use the same player that Shelter does, so it has all of the same stats, etc, rather than creating a new player in Church (which is what I did in Shelter.h). 我希望教会使用与Shelter相同的球员,所以它具有所有相同的统计数据,等等,而不是在Church中创建新球员(这就是我在Shelter.h中所做的)。

Right now, I have something like: 现在,我有类似的东西:

class Church
{
Church();
Shelter **cplayer; // This is supposed to be the pointer to the pointer.
};

The default constructor is where I'm having my problem. 默认构造函数是我遇到问题的地方。 I want to use the same player from Shelter, not create a new player like I did in Shelter. 我想使用Shelter中的同一播放器,而不是像在Shelter中那样创建新播放器。

Church::Church
{
    What should I do here?
}

I've tried a number of things, but I can't quite get it working. 我已经尝试了很多方法,但是无法完全正常工作。 Eventually I want to be able to do something like this: 最终,我希望能够执行以下操作:

   player->adjustHealth(-1);    // Subtract one health from the player. 

Only in Church, so that player's stats, like health, are adjusted no matter which location they are in. 仅在教堂内,因此无论球员身在何处,都可以对其健康状况等属性进行调整。

I hope my question makes sense. 我希望我的问题有道理。 If not, I can try to clarify better. 如果没有,我可以尝试澄清。 Anyway, thanks in advance. 无论如何,预先感谢。

The problem I'm facing is with creating another class called Church, that is in a separate header file and acts as a separate location in the game. 我面临的问题是创建另一个名为Church的类,该类位于单独的头文件中,并在游戏中充当单独的位置。 I want Church to use the same player that Shelter does, so it has all of the same stats, etc, rather than creating a new player in Church 我希望教会使用与Shelter相同的球员,因此它具有所有相同的统计数据,等等,而不是在教会中创建新球员

This sounds like an ideal situation to use std::shared_ptr . 这听起来像使用std::shared_ptr的理想情况。

class Shelter
{
    Shelter();
    std::shared_ptr<Player> player;     // Create a pointer to Player class.

    // Provide a public accessor
    public:
       std::shared_Ptr<Player> getPlayer() const { return player; }
};

and

class Church
{
    Church(std::shared_ptr<Player> pl) : player(pl) {}
    std::shared_ptr<Player> player;

    // You haven't explained in your post why you need this.
    // Maybe you don't need it.
    // Shelter** shelter;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM