Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this yourself, but make sure you know what you're doing.</p> <h1>How:</h1> <p>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 <strong>static</strong> member variables and functions (i.e. functions that belong to the whole class, rather than to individual objects of the class).</p> <p>So you can use some kind of table or other data structure to store a reference to each object. Like so:</p> <pre><code>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&amp;) { 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&amp; operator=(const A&amp;) { 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&lt;int, A*&gt;&amp; 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&lt;int, A*&gt; 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; } </code></pre> <p>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".</p> <h1>Why:</h1> <p>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 <strong>in scope</strong> 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.</p> <h1>Why not:</h1> <p>The picture gets complicated when inheritance is involved.</p> <ul> <li>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?</li> <li>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?</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload