Function To Gather A Class?

This is a discussion on Function To Gather A Class? within the C++ Programming forums, part of the General Programming Boards category; Is there a function that would gather all objects of one class?...

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Function To Gather A Class?

    Is there a function that would gather all objects of one class?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i don't believe so...

    you mean for garbage collection? i think the only thing you can do (inside of c++) is to organize your objects better. (maybe a linked list?)

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    In the Ctor of your class you can store pointers to the generated object in a list:

    Code:
    Class::Class(...)
    {
      objects.push_back(this); //objects is a list<Class*>
      ...
    }
    
    Class::~Class()
    {
      objects.erase(objects.find(this));
    }
    
    static const list<Class*>& Class::getAllObjects() const
    {
      return objects;
    }
    or something like that
    Hope you don't mind my bad english, I'm Austrian!

  4. #4
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    I meant...

    I meant to gather as in if personx=anyoftheobjectsx || if persony=anyoftheobjectsy...

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Place all classes in an array/linked list, then traverse it checking if any of them equals what you're checking against
    Code:
    #define NrOfEnemies 14
    
    class ENEMY
    {
       ...
    };
    
    class PLAYER
    {
       ...
    };
    
    int main()
    {
       PLAYER Player;
       ENEMY Enemy[NrOfEnemies];
       ...
    
       for(int i=0; i<NrOfEnemies; i++)
       {
          if(Player.GetPosition() == Enemy[i].GetPosition())
          {
             //Do something
          }
       }
    
       ...
    
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21