Array of different classes with member access.

Is it possible to make an array of classes while still having access to their members?

Say I have 3 classes that all have the same members but the member functions all do something different.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class entity1
{
public:
 int x,y;

 onSpace()
 {
  x++;
 }
};

class entity2
{
public:
 int x,y;

 onSpace()
 {
  y++;
 }
};


Could I put those both in the same array and still have them work? I tried an array of void pointers but that doesn't give you access to the members.

Thanks,
Tyler
Sounds like you want polymorphism:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Parent
{
public:
  virtual ~Parent() { }
  virtual void onSpace() = 0;  // pure virtual function
};

class Entity1 : public Parent  // derive from parent
{
  //..
  virtual void onSpace() { /* do something specific to Entity1 here*/ }
};

class Entity2 : public Parent  // derive from parent
{
  //..
  virtual void onSpace() { /* do something specific to Entity2 here*/ }
};

//....

int main()
{
  Parent* array[4];
  array[0] = new Entity1;
  array[1] = new Entity2;
  array[2] = new Entity1;
  array[3] = new Entity2;

  for(int i = 0; i < 4; ++i)  // can call onSpace in all of them
    array[i]->onSpace();  // it will appropriately call the Entity1 or Entity2 version
                     //  depending on which type of object array[i] is.
}
Fasinating. I'll have to look more into that but this should work perfect thanks.

Edit:
Looks like I broke it in some way.
I got the error :
cannot instantiate abstract class due to following members:
see declaration of 'base_entity::Draw'

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class base_entity //copy me for entities
{
public:
  //==Variables==//

  //=============//

  //==Actions====//
  virtual ~base_entity() {}

  //virtual void Collision_Check() = 0;
  //virtual void OnCollision_() = 0;

  //virtual void onKey_Space() = 0;

  //virtual void onClickL() = 0;
  //virtual void onClickR() = 0;
  //virtual void onClickM() = 0;

  //virtual void onMouseOver() = 0;

  virtual void Draw() = 0;
  //=============//
};

class emitter_entity: public base_entity
{
public:
  int x,y;
  int w,h;
  int source_x, source_y, source_w, source_h;
  int anim_count_x, anim_count_y;
  int up_anim_limit_x, up_anim_limit_y;
  int low_anim_limit_x, low_anim_limit_y;
  color_map tint;
  int scale_w, scale_h;
  bool visible;
  int layer;
  ALLEGRO_BITMAP *entity;
  int rot;
  //==Actions====//
  emitter_entity::emitter_entity()
  {
    x=0; y=0;
    w=32; h=32;
    source_x=0; source_y=0; source_w=32; source_h=32;
    anim_count_x=0; anim_count_y=0;
    tint.r=255; tint.g=255; tint.b=255;
    scale_w=0; scale_h=0;
    visible=0;
    layer=0;
    entity=NULL;
    up_anim_limit_x=0; up_anim_limit_y=0;
    low_anim_limit_x=0; low_anim_limit_y=0;
    rot=0;
  };

  //virtual void Collision_Check()
  //{

  //};
  //virtual void OnCollision_()
  //{

  //}

  //virtual void onKey_Space()
  //{

  //}

  //virtual void onClickL()
  //{

  //}
  //virtual void onClickR()
  //{

  //}
  //virtual void onClickM()
  //{

  //}

  //virtual void onMouseOver()
  //{

  //}

  virtual void Draw()
  {
    al_draw_tinted_scaled_rotated_bitmap_region(
      entity,
      source_x,
      source_y,
      source_w,
      source_h,
      al_map_rgb(tint.r,tint.g,tint.b),
      w/2,
      h/2,
      x+w/2,
      y+w/2,
      1,
      1,
      rot,
      0
  );
  //=============//
  }
};
Last edited on
Topic archived. No new replies allowed.