pointer array access

I've prtogrammed a little game.
it has multiple classes thaht represent multiple objects in the game (player,enemies, walls...).
all the classes are based on a subclass.
all objects are registred in an array of the subclass, so i can acess each object through one array.
with a virtual function called handle.
each class has its own handle.

the Problem is:

the clases have unique attributes like lives or kills.

so far i just added them to the subclass.

but now it seams that it wastes to much memory.

how can i acess to the uniqe attributes, without writing them in the subclass or access the objects directly?



sorry for the confuging title
I don't understand you.
I want everything in one bag. But I don't want the bag to be heavy.

sorry for the confuging title
Then don't do it.
If the attributes are unqiue to their respective classes, you should be adding them to the class, not the subclass.

Does your virtual "handle" function return a pointer to the proper class?
If so, you should be able to access the unique attributes and functions through that pointer.

Posting some code might help in understanding what you're having problems with.
okay this is the sub class:
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
#pragma once

#include "defines.h"
#include "Input.h"
#include <math.h>

class gobject
{
public:
	bool isshadow;
    int solidity;
    model mod;
	int typenum;
	int subclass;
	int shadowg;
	vector p;
	vector a;
	vector s;
	vector speed;
	static int number;
	int id;
	bool active;
	virtual void handle();
	gobject(void);
	~gobject(void);
	virtual void handlerender();

};



and this is one of the two child classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class solid :
	public wobject
{
public:



	solid(void);
	void handle();
	~solid(void);
	void handlerender();

};



the gobject constructor automaticly adds the onject to the mainarray.

but how can i return a pointer in the child class?
I mean so that i can return any class?
Last edited on
Your solid class does not derive from gobject, unless wobject is a typo.

Assuming you have a pointer to a gobject instance:
 
gobject *  gobjptr;

And you know the object you're dealing with is a solid, simply cast it to become a pointer to the derived type.
1
2
solid * solidptr;
solidptr = dynamic_cast <solid *> (gobjptr);


edit:
BTW, your vectors need to be specialized, such as:
 
vector<int> speed;


Last edited on
i completly forgot the inter-class between gobject and solid.
It represent evrything that directly represent ssomrthing in the world(tree,player,wall)

And vector is my own class with functions like length() or normalize().



Thanks, I will try it out
sorry but i get this error in the console:
segmentation fault (core dumped)

to this test 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
#include "stdio.h"

class under{
public:    under();
    virtual under * getpointer(){
            return this;
    }
   };

under * mainarray[12];
under::under(){
        static int i=0;
        mainarray[i]=this;
        i++;

    }
class over:under{

public:
    int i;
    virtual over * getpointer(){
            return this;
    }
     int geti ()const{
            return i;
    }
    over(){
                i=9;
    }


};

int main(){
    over o;
    o.i=8;
    over * ptr=dynamic_cast <over *> (mainarray[0]);
    printf("%i\n",ptr->i);
}

Topic archived. No new replies allowed.