At first I read, and now I know that the function of a virtual template element (for now?) Is possible in C ++. A workaround would be to make the class a template, and then use the argument template also in the member function.
But in the context of OOP, I found that the example below would not be very “natural” if the class was actually a template. Note that the code does not actually work, but gcc-4.3.4 reports: error: templates may not be 'virtual'
#include <iostream>
So, making "Fish <Amount> foo" looks weird. However, it seems to me desirable to provide an arbitrary amount of food for each animal.
So I'm looking for a solution on how to achieve something like
Fish bar; bar.eat( SomeAmount food );
This becomes especially useful when viewing a for loop. It would be possible to feed a certain amount (FoodAmount) to all different animals (via eat () and bind1st (), for example), it cannot be done so easily, although I am wounded I find it very intuitive (and therefore to some extent "Naturally "Although some may now object that this is due to a" homogeneous "-characteristic vector of the vector, I think / want it to be possible to achieve this, and I really would like to know how, since it has puzzled me for quite some time. ..
[EDIT]
To possibly clarify the motivation for my question, I want to program the Exporter class and give it different, more specialized classes. While the top-level Exporter class is usually for cosmetic / structural purposes only, the GraphExporter class is rendered, which should again serve as the base class for an even more specialized export. However, like the Animal example, I would like to be able to define GraphExporter * even on specialized / derived classes (for example, on SpecialGraphExplorer), but when calling "write (out_file)" it should call the corresponding member function for SpecialGraphExporter of GraphExporter :: write (out_file).
Perhaps this makes my situation and intentions clearer.
Best
Shadow
c ++ virtual templates
Shadow May 03 '11 at 15:16 2011-05-03 15:16
source share