Passing an array to a class
Here's my problem... i have a 8x8 array of integers... and i need to have 24 seperate instances of a class to access it. What would be the best, fastest, most efficient way to accomplish this task... i need to hand it to a function one time each game loop cycle.
I figured passing a pointer to the array, but if I do that how would i access the array?
I'm sorry for being such a bother. I'm trying to work my way into being comfortable with all aspects of C++.
Is there a way that I could possibly use extern for this... since my array is already declared as global in the main source file... could i get this from the classes, which are defined in other files?
Thanks in advance.
40
www.databyss.com
[a]http://www.omlettesoft.com'>www.omlettesoft.com
"Don't meddle in the affairs of wizards, for they are subtle and quick to anger."
Edited by - 40 Thieves on 8/19/00 12:36:59 AM ' Target=_Blank>Link
-40
An array name is a const pointer to the first element of the array. I *think* you should be able to use an int** to access it, but you might have to declare the array dynamically. This is what I'd do:
if this doesn't work:
int array[8][8];
int** arrayPtr = array; // in the class
you might have to cast it, like so:
int **arrayPtr = (int**)array;
then do this:
NOTE:: ( and ) == [ and ]
int** array;
array = new int(8);
for(int i = 0; i < 8; i++)
array(i) = new int(8);
int** arrayPtr = array; // in the class
// delete the 2d array
for(int i = 0; i < 8; i++)
delete() array(i);
delete() array;
Hope this helps!
Edited by - Qoy on August 19, 2000 1:45:39 AM
if this doesn't work:
int array[8][8];
int** arrayPtr = array; // in the class
you might have to cast it, like so:
int **arrayPtr = (int**)array;
then do this:
NOTE:: ( and ) == [ and ]
int** array;
array = new int(8);
for(int i = 0; i < 8; i++)
array(i) = new int(8);
int** arrayPtr = array; // in the class
// delete the 2d array
for(int i = 0; i < 8; i++)
delete() array(i);
delete() array;
Hope this helps!
Edited by - Qoy on August 19, 2000 1:45:39 AM
the problem with that solution is that i have a 2 dimensional array board[8][8]... how would that work with your solution... and what does a ** do?
-40
Either pass the array by reference or better yet, if your class is the only thing that needs to use it, declare it as a static array for the class:
class CWhatever {public: static int foobar[8][8]; CWhatever(); ... etc ...}
.-Hi!...
.-You can index a pointer pointing to an array as if it was an array(well, arrays are just pointers). Let me explain:
int int_array[15];
int* p;
p=int_array/*WITHOUT &.Because int_array is a pointer to the first element of the array.*/
//this
p[12]=12;
//is the same as :
int_array[12]=12;
.-I´ve now noticed that will only work for 1 dimension array...
.-For more dimensions I´m not sure what to do: Having a number of pointers equal to the dimension and using each to change each dimension row...
.-Expert help is needed here..
.-I hope this helps you, at leat a little...
What the hells!
What the hells!
Arrays are != pointers.
int foobar(int** two_d_array) will NOT work.
For you see,
An array is made a pointer when its passed to a function, but its not recursive, so... its made a pointer to an array of values, not a pointer to a pointer. At least in pure C, but it should be the same in C++. Just pass a reference.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
int foobar(int** two_d_array) will NOT work.
For you see,
An array is made a pointer when its passed to a function, but its not recursive, so... its made a pointer to an array of values, not a pointer to a pointer. At least in pure C, but it should be the same in C++. Just pass a reference.
-----------------------------
A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
For simplicity''s sake i ended up with my array like this:
int Board[8][8];
and my function declaration like this:
void function(int board[8][8], int x);
and i pass the array to my function with this:
function(Board, 5);
That seems to work and stuff.... but I guess it''s a bit of a slowdown.
Thanks everybody though for your help and interesting thoughts
40
www.databyss.com
www.omlettesoft.com
"Don''t meddle in the affairs of wizards, for they are subtle and quick to anger."
int Board[8][8];
and my function declaration like this:
void function(int board[8][8], int x);
and i pass the array to my function with this:
function(Board, 5);
That seems to work and stuff.... but I guess it''s a bit of a slowdown.
Thanks everybody though for your help and interesting thoughts
40
www.databyss.com
www.omlettesoft.com
"Don''t meddle in the affairs of wizards, for they are subtle and quick to anger."
-40
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement