Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To access catridgeClass attributes, you will need a catridgeClass object.</p> <p>First, you need to correct your class to have public attributes.</p> <pre><code>class catridgeClass:public game_list { public: string N_bits; bool is_import; }; class anotherClass: public game_list { public: string other_member; }; </code></pre> <p>Than you need to get a catridgeClass object from the caller function:</p> <pre><code>int main() { std::vector&lt;game_list *&gt; games; games.push_back(new catridgeClass); games.push_back(new anotherClass); for(int i=0; i&lt;games.size(); i++) { //You will need to get a catridgeClass object to access it's members. catridgeClass *catridge_ptr = dynamic_cast&lt;catridgeClass *&gt;(games[i]); //Or to access anotherClass. anotherClass *another_ptr = dynamic_cast&lt;anotherClass*&gt;(games[i]); //If the conversion is not possible, dynamic_cast will return a null pointer if(catridgeClass != NULL) std::cout &lt;&lt; catridge-&gt;N_bits &lt;&lt; std::endln; if(anotherClass != NULL) std::cout &lt;&lt; anotherClass-&gt;other_member &lt;&lt; std::endln; } //And of course, you need to avoid a memory leak for(int i=0; i&lt;games.size(); i++) delete games[i] return 0; } </code></pre> <p>Keep in mind that it's not a desired design. If you are trying to use polimorphism, is because all that classes share common behaviours. A better approach would be to use <a href="http://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm" rel="nofollow">interfaces</a>.</p>
 

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