嵌入式C ++类交互 [英] Embedded C++ Class interaction
问题描述
我正在继续一个嵌入式微控制器(Arduino)的游戏,我有一个关于类互动的问题 - 这个问题从我以前的问题继续这里,我基于我的代码建议sheddenizen(见回复给定的链接在这里):
I'm continuing with a game for an embedded microcontroller (Arduino), and I have a question on class interaction -- this question continues from my previous question here and I have based my code on the suggestion of sheddenizen (see response to the given link in 'here'):
我有三个继承自一个基类的类 -
I have three classes that inherit from a base class-
(i)
(i) class Sprite - (bass class) has a bitmap shape and (x,y) position on an LCD
(ii)class Missile:public Sprite (x,y)并且还采取obj
(ii) class Missile : public Sprite - has a specific shape, (x,y) and also takes a obj
(iii) class Alien:public Sprite
(iv) class Player:public Sprite -
有不同的(虚拟)移动方法,并显示在LCD上:
They all have different (virtual) method of moving and are shown on the LCD:
我的简化代码在下面 - 具体来说,我只想要导弹在某些情况下发射:当导弹是创建它需要一个对象(x,y)值,我如何访问一个继承类中传递的对象值?
// Bass class - has a form/shape, x and y position
// also has a method of moving, though its not defined what this is
class Sprite
{
public:
Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit);
virtual void Move() = 0;
void Render() { display.drawBitmap(x,y, spacePtr, 5, 6, BLACK); }
unsigned int X() const { return x; }
unsigned int Y() const { return y; }
protected:
unsigned char *spacePtr;
unsigned int x, y;
};
// Sprite constructor
Sprite::Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit)
{
x = xInit;
y = yInit;
spacePtr = spacePtrIn;
}
/*****************************************************************************************/
// Derived class "Missile", also a sprite and has a specific form/shape, and specific (x,y) derived from input sprite
// also has a simple way of moving
class Missile : public Sprite
{
public:
Missile(Sprite const &launchPoint): Sprite(&spaceMissile[0], launchPoint.X(), launchPoint.Y()) {}
virtual void Move();
};
void Missile::Move()
{
// Here - how to access launchPoint.X() and launchPoint.Y() to check for
// "fire conditions"
y++;
Render();
}
// create objects
Player HERO;
Alien MONSTER;
Missile FIRE(MONSTER);
// moving objects
HERO.Move();
MONSTER.Move();
FIRE.Move();
推荐答案
因为 Missile
是 Sprite
的子类,您可以访问 Sprite :: x
和 Sprite :: y
,就好像他们是导弹的成员一样。这是通过简单地写 x
(或如果你坚持认为 this-> x
)。
Since Missile
is a subclass of Sprite
you can access Sprite::x
and Sprite::y
as if they were members of Missile. That is by simply writing x
(or this->x
if you insist).
现在,构造函数中的 launchpoint
引用已经消失,因此您的 Missile :: Move <
The launchpoint
reference that you got in the constructor is gone by now, so your Missile::Move
memfunction cannot access it any more.
如果在此期间成员 x
和 y
更改,但您想要原始值,您可以保存对 launchpoint
这可能是危险的,它被毁灭),或者你必须保持原始坐标的副本。
If in the meantime the members x
and y
changed, but you want the original value, you can either save a reference to launchpoint
(which might be dangerous, it is destroyed) or you have to keep a copy of the original coordinates.
这篇关于嵌入式C ++类交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!