-
Typedef class question
Not sure what to call the subject.
Im trying to access a member of a class from an array of them from within the class.
Kinda like this
typedef class cow
{
int a;
int b;
b=CowList[7].a;
}cow;
cow CowList[10];
Is there such thing as a Variable prototype or something of that nature.
-
Give an example of how you are intending to use it.
-
Never typedef something with the exact name of the class. That's just ugly and, well, wrong:
Code:
typedef class Foo Foo;
Don't do the above. You would need to give them different names.
You cannot have a class contain an instance of itself. Think of that. If ClassA contains ClassA, then that instance of ClassA contains an instance of ClassA. And that instance of ClassA contains an instance of ClassA, and that...
See the problem?
Quzah.
-
Code:
class myClass{
int a;
int b;
int c[10];
};
int main(){
myClass anarray[20];
int number;
//memorys foggy.... does this create the objects for you????
meClass* anarray_ofpointers[10];
number = anarray[5].a;
number = anarray[2].b;
number = anarray[1].c[5];
number = anarray_ofpointers[5]->a;
}
-
Quote:
Originally posted by Nor
Code:
//memorys foggy.... does this create the objects for you????
meClass* anarray_ofpointers[10];
Nope. Creating a pointer just gives you a pointer, not an instance.
Quzah.
-
To expand, creating an array of pointers will allocate space for the pointers themselves. Not anything they point at, and the pointers will have random values unless you initialize to NULL.
-
Sorry I wrote that example pretty quick. Im trying to Check to see if both my Objects are on the same X, Y position.
class ENTITY
{
int x;
int y;
int DetectCollision()
{
int i;
for(i=0;i<100;i++)
{
if(COW[i].x==x&&COW[i].y==y)
return i; //Return which one it collided with
}
return -1; //otherwise return -1
}
};
ENTITY COW[100];
I hate not being able to tab on these forums!!! cant post whole class its huge and would really suck to go through but this is the basic problem.
-
Redsign. You are doing this wrong. You should make a seperate class for collisions. Either that, or make the function take an array of classes as an argument and pass it that way.
But that's just an ugly way to do it. You should make a seperate class for collisions.
Quzah.