C ++双指针成员访问

时间:2013-12-22 18:32:22

标签: c++ pointers double member

C ++(Arduino包装)问题:我正在Arduino上写一个连接LCD的射击游戏 -

我有一个基类(Sprite),其他类来自 - AlienMissilePlayer。 Alien类的构造函数也有私有成员pMissile(指向Missile类的指针) - “对象中的对象”将是一种描述这种情况的方法。 [当Alien发射导弹时,它将自己的(x,y)坐标传递给导弹,导弹有自己从Alien坐标开始移动的方法

我的问题是:如何通过Alien对象访问导弹的坐标? 简化的代码如下,我还绘制了类的表示:

// Bass class - has a form/shape, x and y position  
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 getX()    const { return x; } 
    unsigned int getY()    const { return y; }
  protected:
    unsigned char *spacePtr;
    unsigned int x, y;
};


// Derived class "Missile", also a sprite and has a specific form/shape, and specific (x,y) derived from input sprite
class Missile : public Sprite
{
public:
   Missile(): Sprite(&spaceMissile[0], 0, 0) {}
   virtual void Move();  // its own method of moving
};

// Derived class "Alien" - has a specific form/shape, and specific (x,y) position
class Alien : public Sprite
{
public:
   Alien(); 
   virtual void Move();   // its own method of moving
private:
   Missile *pMissile;
};

Alien::Alien(): Sprite(&spaceAlien[0], random(5, 75), random(4, 10))
{
  Missile MissileArray[MaxAmmoSize];
  pMissile = &MissileArray[0];
}


void Alien::Move()
{ 
  if( random(10) % 2 == 0 )
    x += 1;
  if( random(10) % 3 == 0 )
    y += 1;

  if( (pMissile != NULL) && (random(10) == 1) )
  {
    pMissile->setCoord(x, y);
    pMissile->Move();  // move the missile   
    pMissile++;        // move onto the next missile in the array
  }
  Render();
}

/*****************************************************************************************/
Alien MONSTER;
Player HERO;
Alien *pMONSTER = &MONSTER;

void loop()
{
  display.clearDisplay();
  MONSTER.Move();
  HERO.Move(); 
  pMONSTER->getX(); // this is x location of MONSTER
  **// how does pMONSTER access pMissile(x,y) within MONSTER.** 
  delay(100);
  display.display();
}

enter image description here

Embedded C++ Class interaction

2 个答案:

答案 0 :(得分:1)

常见的方法是向Alien添加一个getter函数:

class Alien {
public:
    Missile* getMissile() { return pMissile; }
}

使用它:

Alien* a = getAlienFromSomewhere();
auto pMissile = a.GetMissile();
if (pMissile != NULL) {
    x = pMissile->getX();
    y = pMissile->getY();
}

答案 1 :(得分:0)

我想你想要通过外星人来检查你的导弹位置以测试与你的英雄实体的碰撞,但是如果你需要保持你的导弹的轨道,你不应该用你指向下一个导弹的指针“行走”显示在Alien::Move()中。这样做会丢失数组开头的引用。

恕我直言,我会在你的外星人阶级做这样的事情:

// Bass class - has a form/shape, x and y position  
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& getX()    const { return x; } 
    unsigned int& getY()    const { return y; }
  protected:
    unsigned char *spacePtr;
    unsigned int x, y;
};


// Derived class "Missile", also a sprite and has a specific form/shape, and specific (x,y) derived from input sprite
class Missile : public Sprite
{
public:
   Missile(): Sprite(&spaceMissile[0], 0, 0) {}
   virtual void Move();  // its own method of moving
};

// Derived class "Alien" - has a specific form/shape, and specific (x,y) position
class Alien : public Sprite
{
public:
   Alien(); 
   ~Alien(); // a destructor to cleanup your missiles - arduino have almost no memory to handle leaks ;-) 
   virtual void Move();   // its own method of moving

   inline Missile& getMissile(unsigned char n) { return pMissile[n];          }
   inline Missile& operator[](unsigned char n) { return getMissile(n);        }
   inline unsigned int& getX(unsigned char n)  { return getMissile(n).getX(); }
   inline unsigned int& getY(unsigned char n)  { return getMissile(n).getY(); }

private:
   Missile *pMissile;
   // adding the code to handle the count
   unsigned char missileCount;
};

Alien::Alien(): 
  Sprite(&spaceAlien[0], random(5, 75), random(4, 10)),
  missileCount(0)
{
  // this way of allocation creates a local object that is destroyed by the end of this scope
  //Missile MissileArray[MaxAmmoSize];
  //pMissile = &MissileArray[0];

  // so you should do somethin like this
  pMissile = new Missile[MaxAmmoSize];
}

Alien()::~Alien()
{
  delete[] pMissile;
}

void Alien::Move()
{ 
  if( random(10) % 2 == 0 )
    x += 1;
  if( random(10) % 3 == 0 )
    y += 1;

  if( (pMissile != NULL) && (random(10) == 1) )
  {
    // my proposal to fire it up
    Missile& missile = pMissile[missileCount];
    missile->setCoord(x, y);
    missile->Move();  // move the missile   
    missileCount++;   // move onto the next missile in the array
  }
  Render();
}

使用这样的代码,您可以使用以下方式访问导弹的位置:

MONSTER.getX(0) += 1;
MONSTER[0].getY() +=1;
MONSTER.getMissile(1).getX() = 10;

为了清楚起见,我还建议将getX()getY()方法重构为x()y(),因为它们会返回对类内容的引用(和这样做,您还应该将xy成员重命名为其他内容,否则您可能会因名称冲突而疯狂。