【问题标题】:How to pass variables values between classes如何在类之间传递变量值
【发布时间】:2016-03-13 11:14:37
【问题描述】:

所以我做一个小项目只是为了好玩(因为在我的课堂上我们根本没有做任何有趣的事情)试图复制旧 RPG 的战斗系统,我遇到了这个问题:我有 2类,一个需要另一个类的变量的值。虽然我还是有点新,所以请不要把任何事情视为理所当然。代码如下:

#include<iostream>
#include<string>
#include<stdlib.h>
#include<time.h>

class Moveset
{
    int pp;
    int precision;
    std::string moveName;
public:
    bool hit(int precision)
    {
        srand(time(0));
        int randomNumber = rand() % 100 + 1;
        if (randomNumber <= precision && randomNumber >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    void scratch(float &HP)
    {
        pp = 15;
        precision = 80;
        moveName = "scratch";

        bool success = hit(precision);


        //here i want to replace the question marks with the playerName from
        //the player class, but how can i do that?

        std::cout << "??? used " << moveName << " !" << std::endl;
        pp--;

        if (success)
        {
            HP = HP - 20;

            std::cout << "HP: " << HP << std::endl;
        }
        else
        {
            std::cout << "It missed!" << std::endl;
        }
    }

};

class Player
{
public:

    float HP;
    std::string playerName;
    int age;
    Moveset ptr0;
};

int main()
{

    for (int i = 0; i <= 2; i++)
    {
        switch (i)
        {
        case 0:
            std::cout << "Welcome to the test of this special combat system!" << std::endl;
            break;
        case 1:
            std::cout << "In this small test you'll have access of a prototype of it!" << std::endl;
            break;
        case 2:
            std::cout << "Now get ready and experience the first version ever! Go!" << std::endl;
            break;
        default:
            break;

        }
        system("pause");
        system("cls");
    }

    Moveset Move;
    Player Niko;
    Niko.HP = 100;
    Niko.playerName = "Niko";
    Niko.ptr0 = Move;
    Niko.ptr0.scratch(Niko.HP);
    system("pause");

}

【问题讨论】:

  • 您具体想要什么?上面的代码能编译吗?如果它编译,它做了什么你不喜欢的?如果无法编译,请发布确切的错误消息。
  • Nono 代码现在可以正常工作,我想要的是代替问号(“??? used”)我想说的是玩家类中的实际 playerName但我不知道如何做到这一点而不会导致错误。

标签: c++ class oop variables


【解决方案1】:

更好的使用对象,像这样:

void scratch(Player &p)

因此,您将拥有所有玩家数据,而不仅仅是 HP。

您将在 PlayerMoveset 类之间存在交叉依赖关系,但这是另一个问题,您也可以在它自己的 Moveset 中保留对 Player 的引用,或者使用接口.

【讨论】:

  • 然后你会说Niko.ptr0.scratch(Niko);来调用它
  • 这可能有效,但问题是,由于玩家在从头开始后被清除,结果是 p 未被清除,并且由于我在使用 Moveset 类的播放器类中清除了一个指针,所以我无法放置它在 Moveset 类之前,除非我删除它并更改整个代码,但遇到指向类的指针是我的目标,所以这不是我想做的:/ .
  • 调用 ptr0 不会使其成为指针。您尚未在代码中的任何位置声明任何指针(您使用&lt;type&gt;*&lt;name&gt; 声明了一个指针)。您分配 Niko.ptr0 = Move 调用 Moveset 的隐式复制构造函数,因此没有任何内容未声明。如果您想体验指向对象的指针,您需要将Moveset ptr0 替换为Moveset * ptr0 并将您的访问权限更改为Niko.ptr0-&gt;scratch(Niko.HP)
  • 另外,如果您担心在 Moveset 类中引用 Player 类,那么只需使用前向声明即可。将Player 类移到Moveset 上方,然后在Player 类定义之前添加class Moveset; 前向声明。如上所述,您需要将ptr0 更改为实际指针。这是因为Player 如果不需要知道它的大小(所有指针的大小相同),它只能将Moveset 包含为不完整的类型。然而,有更好的方法来编写这样的东西,我会看看整个方法。
  • 我正在寻求改进,那么您认为在这种情况下最好的方法是什么? (非常感谢)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
相关资源
最近更新 更多