Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the OP is interested in a solution to his original problem even if it uses a different method than the one he attempted. Here is a simple example of how to add an object to a global list when it is constructed, and remove it when it is deleted. One thing to remember: you must call AddList in every constructor you add to your base class. I didn't know whether you want the list to be accessible outside the class or not, so I added getter functions to return non-const iterators to the list.</p> <pre><code>class MyClass { private: static std::list&lt;MyClass*&gt; mylist; std::list&lt;MyClass*&gt;::iterator mylink; // disable copy constructor and assignment operator MyClass(const MyClass&amp; other); MyClass&amp; operator = (const MyClass&amp; other); void AddList() { mylink = mylist.insert(mylist.end(), this); } void RemoveList() { mylist.erase(mylink); } public: MyClass() { AddList(); } virtual ~MyClass() { RemoveList(); } static std::list&lt;MyClass*&gt;::iterator GetAllObjects_Begin() { return mylist.begin(); } static std::list&lt;MyClass*&gt;::iterator GetAllObjects_End() { return mylist.end(); } virtual std::string ToString() const { return "MyClass"; } }; class Derived : public MyClass { virtual std::string ToString() const { return "Derived"; } }; std::list&lt;MyClass*&gt; MyClass::mylist; int main() { std::vector&lt;MyClass*&gt; objects; objects.push_back(new MyClass); objects.push_back(new MyClass); objects.push_back(new Derived); objects.push_back(new MyClass); for (std::list&lt;MyClass*&gt;::const_iterator it = MyClass::GetAllObjects_Begin(), end_it = MyClass::GetAllObjects_End(); it != end_it; ++it) { const MyClass&amp; obj = **it; std::cout &lt;&lt; obj.ToString() &lt;&lt; "\n"; } while (! objects.empty()) { delete objects.back(); objects.pop_back(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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