我與嵌入式微控制器(Arduino的)比賽繼續進行,我對課堂互動的一個問題 - 這個問題從我剛纔的問題here繼續,我已經根據的建議,我的代碼sheddenizen(見對給定鏈路在「這裏」):嵌入式C++類的互動
我有一個從類 - 基本繼承三類
(I)類雪碧 - (貝司類)有一個位圖的形狀和在LCD上(X,Y)位置
(ⅱ)類導彈:公共的Sprite - 具有特定的形狀,(X,Y),並且還需要一個物鏡
(ⅲ)類外來:公共的Sprite - 具有特定的形狀和(X,Y)
(IV)類球員:公共雪碧 - 「」
他們都有着動人的不同(虛擬)方法,並顯示在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();
你問如何在已傳遞給另一個函數一個函數訪問的變量。這是不可能的:你確定這是你想要的嗎? – suszterpatt