Is it possible to have an array of different objects that come from the same base class?

I am trying to create a weapon class for a game. This is part of the code that I came up with to fit my needs:

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();
};

And then I would like to define an array of weapons as follows:

//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons];

I would like element [0] to represent the class machineGun and element [1] to represent the class flamer. Am I doing this problem right? Should I somehow reorganize this? How to reach the array in which these two different types of weapons are stored?

The idea is that when I call weapons[0].fire();, I get the machineGun class, and when I call weapons[1].fire();, I get fire fire.




: , . " [0] = new machineGun;". " 0" .

- , ? :

//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons];
weapons[0] = new machineGun;
weapons[1] = new flamer;

:

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




. , .

//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons] = {new machineGun, new flamer};

:

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'




:

//Weapon variables
const int totalWeapons = 2;
int currentWeapon = 1;
weapon *weapons[totalWeapons] = {new machineGun, new flamer};

, !

+5
4

, *, .

,

weapon* weapons[totalWeapons];
weapons[0] = new machineGun;

, , , , .

+2
#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;

}
+4

fire() - , , .

virtual void fire();

, weapon[0] = new flamer(); - .

, - , . (, , C-, , , ).

(: , .)

+2

. , . , , . , "" , , . , . , weapon* weapons[total weapons]; , weapons[numweapon]=new flamer(params); .

+2

All Articles