Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't work with templates, but I think:</p> <p>(1) You cannot use templates inside a class, templates are more like global types or global variables.</p> <p>(2) In O.O.P., the same problem you present, and that you are trying to solve by using templates, can be solved by using inheritance.</p> <p>Classes work similar to templates, you can extended by adding new things, or replace things of classes with pointers, pointers to objects (A.K.A. "references") and overriding virtual functions.</p> <pre><code>#include &lt;iostream&gt; struct Animal { virtual void eat(int amount ) { std::cout &lt;&lt; "I eat like a generic Animal." &lt;&lt; std::endl; } virtual ~Animal() { } }; #if 0 // example 1 struct Wolf : Animal { virtual void eat(int amount) { std::cout &lt;&lt; "I eat like a wolf!" &lt;&lt; std::endl; } }; struct Fish : Animal { virtual void eat(int amount) { std::cout &lt;&lt; "I eat like a fish!" &lt;&lt; std::endl; } }; #else // example 2 struct AnimalFood { virtual int readAmount() { return 5; } virtual void showName() { std::cout &lt;&lt; "I'm generic animal food" &lt;&lt; std::endl; } }; struct PredatorFood : AnimalFood { virtual int readAmount() { return 500; } virtual void showName() { std::cout &lt;&lt; "I'm food for a predator" &lt;&lt; std::endl; } }; struct Fish : Animal { virtual void eat(AnimalFood* aFood) { if (aFood-&gt;readAmount() &lt; 50) { std::cout &lt;&lt; "OK food, vitamines: " &lt;&lt; aFood-&gt;readAmount() &lt;&lt; std::endl; } else { std::cout &lt;&lt; "too much food, vitamines: " &lt;&lt; aFood-&gt;readAmount() &lt;&lt; std::endl; } } }; struct Shark : Fish { virtual void eat(AnimalFood* aFood) { if (aFood-&gt;readAmount() &lt; 250) { std::cout &lt;&lt; "too litle food for a shark, Im very hungry, vitamines: " &lt;&lt; aFood-&gt;readAmount() &lt;&lt; std::endl; } else { std::cout &lt;&lt; "OK, vitamines: " &lt;&lt; aFood-&gt;readAmount() &lt;&lt; std::endl; } } }; struct Wolf : Fish { virtual void eat(AnimalFood* aFood) { if (aFood-&gt;readAmount() &lt; 150) { std::cout &lt;&lt; "too litle food for a wolf, Im very hungry, vitamines: " &lt;&lt; aFood-&gt;readAmount() &lt;&lt; std::endl; } else { std::cout &lt;&lt; "OK, vitamines: " &lt;&lt; aFood-&gt;readAmount() &lt;&lt; std::endl; } } }; #endif int main() { // find animals Wolf* loneWolf = new Wolf(); Fish* goldenFish = new Fish(); Shark* sharky = new Shark(); // prepare food AnimalFood* genericFood = new AnimalFood(); PredatorFood* bigAnimalFood = new PredatorFood(); // give food to animals loneWolf-&gt;eat(genericFood); loneWolf-&gt;eat(bigAnimalFood); goldenFish-&gt;eat(genericFood); goldenFish-&gt;eat(bigAnimalFood); sharky-&gt;eat(genericFood); sharky-&gt;eat(bigAnimalFood); delete bigAnimalFood; delete genericFood; delete sharky; delete goldenFish; delete loneWolf; } </code></pre> <p>Cheers.</p>
    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. 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.
    3. 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