Thread: Typedef class question

  1. #1
    super-mutt
    Guest

    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.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    Question

    Give an example of how you are intending to use it.
    "Borland Rocks!"

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    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;
        
    }
    Last edited by Nor; 07-28-2003 at 02:00 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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.

  7. #7
    super-mutt
    Guest
    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.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Allegro Programming Question
    By xmltorrent in forum Game Programming
    Replies: 2
    Last Post: 08-24-2006, 02:45 PM
  4. typedef class and class
    By frenchfry164 in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2003, 01:32 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM