Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting all objects of a classes in C++

Tags:

c++

object

class

Is there a way through which we can get all the objects of a class in C++.Like in Python we can do

class_name.objects.all() 

to get all the objects of a class.What's its analog in C++,if it exists?

like image 732
itsrishre Avatar asked Jul 07 '13 05:07

itsrishre


People also ask

How do I get all instances of class in C++?

There's no magic way to access all instances of a class. If you want to do that, you'll need to maintain some kind of container (e.g. a vector) of all the instances you've created, and then iterate over that and call the method on each one.

How do you access data members of a class?

Accessing data members and member functions: The data members and member functions of class can be accessed using the dot('. ') operator with the object.

How do you keep track of how many objects of a given class exist?

By putting the static variable with an increment operator in the constructor method, we can increment the value by 1 each time the constructor is called (when a new object is created). This allows us to keep track of the number of objects that have been created for a class.

Are there objects and classes in C?

The main purpose of Objective-C programming language is to add object orientation to the C programming language and classes are the central feature of Objective-C that support object-oriented programming and are often called user-defined types.


1 Answers

You can do this yourself, but make sure you know what you're doing.

How:

There's nothing within C++ that already does this, but it's pretty easy to do this yourself. The key is to recognize that a class can have static member variables and functions (i.e. functions that belong to the whole class, rather than to individual objects of the class).

So you can use some kind of table or other data structure to store a reference to each object. Like so:

class A {

public:
  //constructor assigns this object an id based on the static value curID,
  //which is common to the class (i.e. the next class to call the constructor 
  //will be assigned an id thats 1 more than this one
  //also, constructor adds the pointer to this object to a static map of ids 
  //to objects. This way, the map can be queried for the pointer to an object 
  //that has a particular id
  A() { 
    id = curID++;
    objects[id] = this;
  }

  //copy constructor ensures an object copied from another does not 
  //take the id of the other object, and gets added to the map
  A(const A&) {
    id = curID++; //don't want have the same ID as the object we are copying from
    objects[id] = this;
    x = A.x;
    y = A.y;
  }

  A&  operator=(const A&) {
    id = curID++;
    objects[id] = this;
    x = A.x;
    y = A.y;
    return *this; 
  }

  //destructor removes the pointer to this object from the map
  ~A() {
    objects.erase(id);
  }

  //function to get the map that stores all the objects
  static map<int, A*>& GetMapOfObjects() { 
    return objects;
  }

private:
  //the following variable is **static**, which means it does not
  //belong to a single object but to the whole class. Here, it is
  //used to generate a unique ID for every new object that's 
  //instantiated. If you have a lot of objects (e.g. more than
  //32,767), consider using a long int
  static int curID;  

  //this variable is also static, and is map that stores a pointer
  //to each object. This way, you can access the pointer to a
  //particular object using its ID. Depending on what you need, you
  //could use other structures than a map
  static map<int, A*> objects; 


  //this is a (non-static) member variable, i.e. unique to each object.
  //Its value is determined in the constructor, making use of curID.
  int id;

  //these are some other member variables, depending on what your object actually is
  double x;
  double y; 
}

Note: The above design is very basic and not complete, but just meant to give you an idea of how to implement what you're asking for using static members/functions. For example, for operations that you want to perform on all the objects, for example, it may be better to implement a static function that iterates through the map of elements, rather than getting the map and then doing the iterations "outside".

Why:

I've never used this method myself, but one potential use case I can think of is e.g. in a graphics or game application, where you may want to only draw objects that are in scope and change certain drawing-related properties of all of them at once, e.g. color or size. I'm working on an application that might eventually need something like this (sort of a visual debugger). I'm sure people can provide more examples in the comments.

Why not:

The picture gets complicated when inheritance is involved.

  • If you have a class B that derives from A (i.e. B "is an" A), then who should keep track of objects of B? A static member of objects in A, or a similar one in B, or both?
  • Let's say both. Then what happens if a static function that applies to all objects in A calls a virtual member function on each object? If the virtual function has been overridden in the derived class, then that function will be called instead for all objects being tracked in class A that are actually B objects. What happens if you then call that function again in another static function in B?
like image 179
maditya Avatar answered Oct 27 '22 17:10

maditya