Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Where is the closing bracket of the for loop? </p> <pre><code>void checkList() { for (int i = 0; i &lt; index ;i++){ next = basePrt[i]; next -&gt; baseFunc(); //fails here } // this one is missing in the example ! } </code></pre> <hr> <p>Use a vector ...</p> <pre><code>class SomeClassUseBase { private: std::vector&lt;Base*&gt; basePrt; public: void register( Base * base ){ basePrt.push_back(base); } void checkList() { std::for_each( basePrt.begin(), basePrt.end(), std::mem_fun(&amp;Base::baseFunc); } </code></pre> <p>};</p> <hr> <p>This works for me. It's not the code i would write for my projects but it works.</p> <pre><code>class SomeClassUseBase; class Base { // This base class is pure abstract public: virtual ~Base() {} // Necessary to trigger destructors in inherited classes virtual void baseFunc() = 0; }; class DerivedA : public Base { public: DerivedA(SomeClassUseBase * tmp){ tmp -&gt; registerIt(this); } void baseFunc(){ // do something here that's only for DerivedA } }; class DerivedB : public Base { public: DerivedB(SomeClassUseBase * tmp){ tmp -&gt; registerIt(this); } void baseFunc() { // do something here that's only for DerivedB } }; class SomeClassUseBase { private: Base ** basePrt; //Remark: use a vector or shared_array unsigned int index; SomeClassUseBase(const SomeClassUseBase&amp;); const SomeClassUseBase&amp; operator=(const SomeClassUseBase&amp;); public: explicit SomeClassUseBase(int num) { //Remark: Store the size of the container basePrt = new Base*[num]; //create an array of pointers to the objects index = 0; } ~SomeClassUseBase() { delete[] basePrt; } void registerIt( Base * base ) { //Remark: what if index &gt;= size of the array? basePrt[index] = base; index = index + 1; } void checkList() const { for (int i = 0; i &lt; index ;i++){ Base *next = basePrt[i]; next-&gt;baseFunc(); } } }; int main() { SomeClassUseBase tmp(5); Base *b[5]; //Remark: Use a smart_pointer for ( int i = 0; i &lt; 5; i += 1 ) { if ( i % 2 == 0 ) { b[i] = new DerivedA(&amp;tmp); } else { b[i] = new DerivedB(&amp;tmp); } // if } // for tmp.checkList(); for(int i = 0; i &lt; 5; ++i ) { delete b[i]; } } </code></pre>
 

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