Advertisement

Pointers to Array in class

Started by February 05, 2004 05:13 AM
6 comments, last by Ructions 21 years, 1 month ago
I am having a problem with pointers. I am using C++ and am making a arkanoid game. My problem is i have set up all my code into class. ie a paddle,ball class. I have a class called TextureLoader which loads in textures and stores them in a array which is private to that class. I have to then get that array within the game class in the draw() function to that i can blind that texture to the quads etc. I am pretty stumpped at the moment so any help would be greatful thanks alot.
Why not make it public?
Advertisement
Or make a public function:

sometype *GetSomeArray(){return thearray;}
I know this may be a stupid question, but being that I see it and don''t understand then I will ask.

How does returning an array like that work?

class bob {public:  int *GetSomeArray() { return thearray; }private:  int thearray[10];};


I presume you would also have a size variable contained in there? so you could do..

int *myArray = bobobject->GetSomeArray();
int theSize = bobobject->GetArraySize();

etc...
If you simply declare it as an array[10] not, else you will. Then you could do this:

unsigned int GetSomeArray(int *a) {
a=thearray;
return thearraysize;
}

This will be faster and uses only one line of code instead of 2 and it looks (my opinion) a lot better.
quote:
Original post by Tree Penguin
If you simply declare it as an array[10] not, else you will. Then you could do this:

unsigned int GetSomeArray(int *a) {
a=thearray;
return thearraysize;
}

This will be faster and uses only one line of code instead of 2 and it looks (my opinion) a lot better.


This doesn''t work since "a" is a copy of what is passed when calling GetSomeArray.
You should do

unsigned int GetSomeArray(int **a) {
*a=thearray;
return thearraysize;
}

and when calling :
int *ptr;
int sz = GetSomeArray( &a );

hammerstein_02 : this works with pointers. When returning you give to the calling context the adress in memory where the array lies.

Advertisement
quote:
This doesn't work since "a" is a copy of what is passed when calling GetSomeArray.
You should do

unsigned int GetSomeArray(int **a) {
*a=thearray;


I don't think this is really neccecery, but if mine doesn't work, this should work:

unsigned int GetSomeArray(int *a) {
a=&thearray[0];

which is exactly a=thearray;

= on a pointer copies the address, so it should work.

[edited by - Tree Penguin on February 5, 2004 7:01:21 AM]
Tree Penguin - the problem is you're not copying out the new value of the array. Example:

Before calling GetSomeArray():
ptr = pointer to int, value = garbage (uninitialised)
Upon calling GetSomeArray():
a = pointer to int, value is the same as garbage, but a is a different object
Upon calling a=thearray;:
a now points to thearray but ptr still points to garbage.
Upon returning from GetSomeArray():
ptr still points to garbage.

The correct way, as MV pointed out is to do:
ptr = pointer to int, value = garbage (uninitialised)
Upon calling GetSomeArray():
a = pointer to ptr
Upon calling *a = thearray;:
a is unchanged, ptr now points to thearray;
Upon returning from GetSomeArray():
ptr points to thearray.

A better implementation would be:
unsigned int getTextureArray(int*& destination){	destination = theArray;	return theArraySize;}   


Although an even better implementation might be:
class TextureManager{	public:		void loadTexture(std::string filename);		GLuint getTexture(std::string name);	private:		std::map<std::string, GLuint> textures;};void TextureManager::loadTexture(std::string filename){	if (textures.find(filename) != textures.end())	{		// already have this texture, no need to load it again		return;	}	/*		insert code here to load the image and		create a texture with id textureId	*/	textures.insert(std::make_pair(filename, textureId));}GLuint TextureManager::getTexture(std::string name){	if (textures.find(filename) == textures.end())	{		// haven't got that texture.  Could try loading it but		// should probably just throw an exception	}	return textures.find(filename)->second;}

I haven't tried compiling that, so there are probably errors, but it gives the general idea. If some on that looks new to you (std::map, e.t.c.) consult your favourite standard library reference.

Enigma

EDIT: missed a newline and cleaned up the first bit.
EDIT2: my final example wa potentially confusing

[edited by - Enigma on February 5, 2004 7:23:21 AM]

This topic is closed to new replies.

Advertisement