是否可以拥有来自同一基类的不同对象数组?

Sam*_*152 5 c++ polymorphism

我正在尝试为游戏设计武器类.这是我提出的满足我需求的一些代码:

class weapon {

 public:
  int fireRate;
  int bulletDamage;
  int range;
  ofImage sprite;
  ofImage bulletSprite;
  bullet bullets[50];
  int activeBullet;

 public:
  void fire();
};

class machineGun: public weapon {
 public: 
  void fire();
};

class flamer: public weapon {
 public: 
  void fire();
};
Run Code Online (Sandbox Code Playgroud)

然后我想定义一个像这样的武器数组:

//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons];
Run Code Online (Sandbox Code Playgroud)

我希望element [0]代表machineGun类,而element [1]代表flamer类.我是否正确地解决了这个问题?我应该以某种方式重构这个吗?我如何实现一个拥有这两种不同武器的阵列?

这个想法是这样的,当我打电话给weapons[0].fire();我得到一个机枪类,当我打电话给weapons[1].fire();我时,我得到了火焰.




编辑:谢谢你的帮助.我在使用"weapon [0] = new machineGun;"时遇到了一些问题.当我尝试运行此代码时,我收到错误"无法分配常量大小为0的数组".

有没有人知道为什么这不起作用?我更新的代码看起来像这样:

//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons];
weapons[0] = new machineGun;
weapons[1] = new flamer;
Run Code Online (Sandbox Code Playgroud)

但是我收到很多错误:

1>gameplay.cpp(49) : error C2466: cannot allocate an array of constant size 0
1>gameplay.cpp(49) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>gameplay.cpp(49) : error C2371: 'weapons' : redefinition; different basic types
1>        gameplay.cpp(48) : see declaration of 'weapons'
1>gameplay.cpp(49) : error C2440: 'initializing' : cannot convert from 'machineGun *' to 'int []'
1>        There are no conversions to array types, although there are conversions to references or pointers to arrays
1>gameplay.cpp(50) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>gameplay.cpp(50) : error C2369: 'weapons' : redefinition; different subscripts
1>        gameplay.cpp(48) : see declaration of 'weapons'
1>gameplay.cpp(50) : error C2440: 'initializing' : cannot convert from 'flamer *' to 'int [1]'
1>        There are no conversions to array types, although there are conversions to references or pointers to arrays
Run Code Online (Sandbox Code Playgroud)




我采取了另一种不同的方法,并得到了一些不同的错误.我仍然不确定这是如何粘合在一起的.

//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons] = {new machineGun, new flamer};
Run Code Online (Sandbox Code Playgroud)

有错误:

1>gameplay.cpp(48) : error C2275: 'machineGun' : illegal use of this type as an expression
1>        gameplay.h(36) : see declaration of 'machineGun'
1>gameplay.cpp(48) : error C2275: 'flamer' : illegal use of this type as an expression
1>        gameplay.h(41) : see declaration of 'flamer'
Run Code Online (Sandbox Code Playgroud)




答案最终是这样的:

//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon *weapons[totalWeapons] = {new machineGun, new flamer};
Run Code Online (Sandbox Code Playgroud)

谢谢所有帮助我解决这个问题的人!

Nav*_*een 5

#include <iostream>

class weapon {

 public:
  int fireRate;
  int bulletDamage;
  int range;
  int activeBullet;

 public:
  virtual void fire(void) {std::cout << "machine " << '\n';}
  virtual ~weapon() {std::cout << "destructor is virtual" << '\n';}
};

class machineGun: public weapon {
 public: 
  void fire(void) {std::cout << "machine gun firing" << '\n';}
  ~machineGun(void) { std::cout << "machine gun destroyed" << '\n';}
};

class flamer: public weapon {
 public: 
  void fire(void) {std::cout << "flamer firing" << '\n';}
  ~flamer(void) {std::cout << "flamer destroyed" << '\n';}
};

int main(void)
{
    const int count = 2;
    weapon *weapons[count];

    machineGun *a = new machineGun();
    flamer     *b = new flamer();

    weapons[0] = a;
    weapons[1] = b;

    weapons[0]->fire();
    weapons[1]->fire();

    delete a;
    delete b;

}
Run Code Online (Sandbox Code Playgroud)


Dem*_*cht 3

正如罗斯所指出的,你的阵列必须是武器*类型,才能允许你向下投射特殊的武器类型。

所以,

weapon* weapons[totalWeapons];
weapons[0] = new machineGun;
Run Code Online (Sandbox Code Playgroud)

另外如上所述,当您动态创建它们时,您需要确保稍后清理它们,否则会遇到内存泄漏