【问题标题】:Setting x or y of a POINT2f/VECTOR2f variable in a class using a member function使用成员函数在类中设置 POINT2f/VECTOR2f 变量的 x 或 y
【发布时间】:2017-02-07 04:36:53
【问题描述】:

我有以下课程:

//----------------------------------------------------------------------
//                              CHARACTER CLASS
//----------------------------------------------------------------------
class Character
{
private:
    POINT2f char_position;

public:
    VECTOR2f char_velocity;

    Character();
    ~Character();

    // Mutators
    void SetChar_Position(POINT2f p);
    void SetChar_Velocity(VECTOR2f v);

    // Accessors
    POINT2f GetChar_Position();
    VECTOR2f GetChar_Velocity();
};

//----------------------------------------------------------------------
//          PLAYER CLASS - INHERITED FROM "CHARACTER" CLASS
//----------------------------------------------------------------------
class Player : public Character
{
private:

public:
int health;

Player();
~Player();

void SetPlayer_Health(int h);

int GetPlayer_Health();
};

所以基本上玩家类继承自角色类。

Character 类的成员函数可以让你设置角色的位置和速度,它接受 POINT2f 和 VECTOR2f。

在我的主要代码中,我创建了一个玩家角色Player Player1,然后在按下按钮时设置球的速度和位置:

if (ui.isActive('A'))   // Check to see if "A" key is pressed
    {
        // Player Ball
        Player1.SetChar_Velocity.x = -12.0;
        Player1.SetChar_Position.x += (Player1.GetChar_Velocity.x*dt);
        Player1.SetChar_Velocity.x += (acceleration*dt);
    }

我目前遇到的错误是 left of .x must have class/struct/union,所以我将 Player1.SetChar_Velocity.x = -12.0; 更改为 Player1.SetChar_Velocity().x = -12.0;,这会导致错误 expression must have class type 和 to few arguments in function call。

有谁知道一种方法可以让我只操纵char_velocity 的x 号。我也知道我的代码 Set .x 速度不正确,因为我没有传入 VECTOR2f 值。

【问题讨论】:

  • Player1.char_velocity.x += ...?顺便说一句,只要 char_velocity 是公开的,您的 SetChar_Velocity 和 GetChar_Velocity 函数就是多余的。
  • 这很好,我只是喜欢有功能,因为在我看来它更简洁一些。那么如果我想更改位于私有的char_position 的x 值呢?
  • 那你必须做POINT2f pos = Player1.GetChar_Position(); pos.x += ...; Player1.SetChar_Position(pos);。不太方便!
  • 公平点,非常感谢您提供的所有信息。

标签: c++ vector member mutators


【解决方案1】:

为了实现你想做的事情,你会希望你的Get_方法的返回类型是一个引用(wiki)。您的代码将如下所示

class Character
{
private:
    POINT2f char_position;

public:
    VECTOR2f char_velocity;

    // Accessors
    POINT2f& GetChar_Position() { return char_position; }
    VECTOR2f& GetChar_Velocity() { return char_velocity; }
};

此外,通常建议或要求能够在代码中保留 const 正确性,因此您可能需要添加

const POINT2f& GetChar_Position() const { return char_position; }
const VECTOR2f& GetChar_Velocity() const { return char_velocity; }

这将允许您进行一些调用,例如

POINT2f current_pos = a_char.GetChar_Position(); // const 
a_char.GetChar_Velocity().x += 2;                // non-const

请注意,通常建议将结构作为 const 引用而不是副本传递(尽管根据您的 c++ 版本和类型,这可能不成立,您可以查看this question 进行说明),因此更改您的

void SetChar_Position(POINT2f pos) { char_position = pos; }
void SetChar_Velocity(VECTOR2f vel) { char_velocity = vel; }

到

void SetChar_Position(const POINT2f& pos) { char_position = pos; }
void SetChar_Velocity(const VECTOR2f& vel) { char_velocity = vel; }

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2020-09-29
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多