pointers classes and some other stuff

Started by
3 comments, last by jLeslie 23 years, 10 months ago
Ok, I have this class, with an array of another class within it. Now I need each class in this array to have a copy of a Matrix stored within the first class. Now is it ok to give each class in the array a pointer to the original matrix? Or am I violating some rule
Advertisement
It depends on what you use the classes to do, how large the array is, whether adding 4 bytes to each member of the array matters, whether you could just pass the 4 bytes to a function when it''s needed, etc. Currently I am making a 2D tile map, and my tile class needs a pointer to the map class so that it can get the tile size, etc. I''m thinking of using a static member for the tile size data, because all the tiles can easily share one copy...that''s already what they are doing (through the map class).

Just post some code or pseudo-code, and show me what you are trying to do.


BTW, this is not a good idea (based on the relative sizes):


class image;
class color;

class image
{
public:
color colors[640 * 480];
};

class color
{
public:
// 24-bit color
unsigned char red;
unsigned char blue;
unsigned char green;

// 32-bit pointer
image* owner;
};



Why isn''t it good? Well, it increases the size of the array by about 133%, and with bitmaps that is just unacceptable. Bitmaps are already too large as it is! Consider passing in the image pointer to a color member function when it is needed, or just think whether or not you actually need the image pointer at all.

Of course, it depends on how large your classes are -- 4 bytes added to large classes in a small array might be trivial.




- null_pointer
Sabre Multimedia
I take it you mean you want each class in the array to all point the same matrix in the first class.

If so, you could use a static matrix in the first class.

class ArrayClass {
public:
// Place what ever data you need !!
};

class FirstClass {
public:
static float f_Matrix[16];
ArrayClass list[???];
};

// Don''t forget to declare the static member outside the class !
float FirstClass::f_Matrix[16];

Now in the ArrayClass functions, when they need to access the matrix of FirstClass then they can access it using:

FirstClass::f_Matrix[0] = ??? or variable = FirstClass::f_Matrix

Hope this helps !!
Michael
Ahhh, I didn''t know a class owned by another class could access parts of it by using its name in front of the variable. Like FirstClass::Matrix.

Thanks.
The symbol :: is called a "Scope Resolution Operator".

It can also be used to access functions and members of a base class during inheritence. Outside of class member functions you can''t access class variables using the scope resolution operator unless they are declared as static.

Michael

This topic is closed to new replies.

Advertisement