简体   繁体   English

C ++将父指针传递给父类构造函数

[英]C++ pass parent pointer to a parent class constructor

I am currently developing a game in C++. 我目前正在用C ++开发游戏。 Now I am a beginner in C++ and am just trying to get used to the concept of multiple inheritance. 现在,我是C ++的初学者,并且正试图习惯于多重继承的概念。 My game has a player class. 我的游戏有一个玩家等级。 That player class inherits from Positionable(gives the player x and y coords) and Moveable(allows the player to move around). 该玩家类继承自Positionable(赋予玩家x和y坐标)和Moveable(允许玩家四处移动)。 The problem is that Moveable has to know the player coords to move him around adequately. 问题在于,Moveable必须了解玩家的协调才能充分地移动他。 Here's basic pseudo(ish) code of what I am trying to accomplish: 这是我要完成的基本伪(ish)代码:

class Movable
{

private:
    Positionable *positionable;

public:
    Movable(Positionable *positionable)
        : positionable(positionable)
    { }

    void Translate(float x, float y)
    {
        positionable->setX(positionable->getX() + x);
        positionable->setY(positionable->getY() + y);
    }
}

class Positionable
{
private: 
    float x, y;
public:
    Positionable(float x, float y)
        : x(x),
        y(y)

    float getX() { return this->x; }
    float getY() { return this->y; }
    float setX(float val) { this->x = val; }
    float setY(float val) { this->y = val; }
}

class Player : public Positionable, public Movable
{
    Player(float x, float y)
        : Positionable(x, y)
        : Movable((Positionable) this)
    { }
}

...

Player player;
player.Translate(50, 50)

My question is is there a better way to pass Positionable to the constuctor of Movable, because if Moveable depended on 3 classes I would have to write something like Movable((FirstClass) this, (SecondClass) this, (ThirdClass) this) . 我的问题是是否有更好的方法将Positionable传递给Movable的构造函数,因为如果Moveable依赖于3个类,则我必须编写类似Movable((FirstClass) this, (SecondClass) this, (ThirdClass) this) This looks bad. 看起来不好 Is there some way I can solve this problem better? 有什么办法可以更好地解决这个问题?

As @BandiKishore suggested in the comments, a better way is to inherit Movable from Positionable (and inherit Player from Movable only). 正如@BandiKishore在评论中建议的那样,更好的方法是从Positionable继承Movable (仅从Movable继承Player )。 Movable needs access to coordinates to do its moving work, and the name Movable suggests that it should move itself ( this ), not some other object (in which case it would be Mover or MovingHelper or smth. like that), so inheritance is more logical than delegation. Movable需要访问坐标才能完成其移动工作,而Movable的名称则建议它应该移动自身( this )而不是其他对象(在这种情况下,它应该是MoverMovingHelper或smth等。),因此继承更多逻辑上比委派。

Player would have to invoke only one base constructor: Player仅需调用一个基本构造函数:

class Movable : public Positionable
{
public:
    Movable(float x, float y) : Positionable(x, y)
    { }
}

class Player : public Movable
{
    Player(float x, float y) : Movable(x, y)
    { }
}

I would turn your IPositionable class into a struct if you are only using it to hold/access X and Y values. 如果仅使用IPositionable类来保存/访问X和Y值,我会将其转换为结构。

If you haven't used structs before, here is a brief overview: http://www.cplusplus.com/doc/tutorial/structures/ 如果您以前没有使用过struct,则简要概述如下: http : //www.cplusplus.com/doc/tutorial/structures/

Now as far as your IMoveable class, I would put an IPositionable member in the class as you did but intialize it in your IMoveable contstructor. 现在,就您的IMoveable类而言,我将像您所做的那样将IPositionable成员放入该类中,但将其初始化到您的IMoveable构造函数中。 Here is a really rough example of what I mean: 这是我的意思的一个真实例子:

    class IMovable
    {

     private:
        IPositionable *positionable;

     public:
        IMovable(int x, int y)
        { 
            positionable->setX(x);
            positionable->setY(y);
        }

        void Translate(float x, float y)
        {
            positionable->setX(positionable->getX() + x);
            positionable->setY(positionable->getY() + y);
        }
    }

then you'll have: 那么您将拥有:

    class Player
    {
        private IMoveable moveable;
        Player(float x, float y)
            : moveable(x, y)
        { }
    }

    ...

    Player player(100, 100);
    player.Translate(50, 50);

This is merely building from your code example. 这仅仅是从您的代码示例构建的。 I did not run this example locally. 我没有在本地运行此示例。

Basically, you do not need to use multiple inheritance to accomplish this task. 基本上,您不需要使用多重继承来完成此任务。 I would suggest not even using inheritance for this task as it is not needed and you should try not to use inheritance if you are not required to! 我建议甚至不要为此任务使用继承,因为不需要它,如果不需要,则应尽量不要使用继承! It is something to be very cautious about. 这是非常谨慎的事情。

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

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