Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questiontemplate <typename T> void func(T x) { ... }
func(4); func<MaClasse>(new MaClass());
void func(int x) { ... } void func(MaClasse x) { ... }
#include class Mother { public: void aff() { std::cout << "mother" << std::endl; } }; class Daughter : public Mother { public: void aff() { std::cout << "daughter" << std::endl; } }; class MotherWithVirtual : public Mother { public: virtual void aff() { std::cout << "mother" << std::endl; } }; class Daughter2 : public MotherWithVirtual { public: void aff() { std::cout << "daughter" << std::endl; } }; int main() { Daughter d; d.aff(); // Affichera bien "daughter" Daughter* b = new Daughter; b->aff(); // Affichera bien "daughter" Mother* m = new Daughter; m->aff(); // N'affichera pas "daughter", mais "mother" MotherWithVirtual* k = new Daughter2; k->aff(); // Affichera bien "daughter" return 0; }
il y a une différence fondamentale
Ne serait-ce pas dans l'appel des fonctions?
Que se passe-t-il quand on appelle une fonction d'un template (point par point) ?
Que se passe-t-il exactement qu'on appelle un fonction virtuelle (étant une classe fille je suppose..) ?
Il doit y avoir un détail qui rend ces deux cas très différents
Pour ce qui est du temps d'exécution, la fonction virtuelle est par définition plus longue à appeler qu'une fonction ou une méthode normale.
Qu'appeles-tu méthodes virtuelles templatés ?
#include template <typename T> class Mother { public: virtual void whoAndWhat(); T t; }; template <typename T> void Mother<T>::whoAndWhat() { std::cout << "Who : Mother" << std::endl << "What :" << t << std::endl; } template <typename T> class Daughter : public Mother<T> { public: virtual void whoAndWhat(); }; template <typename T> void Daughter<T>::whoAndWhat() { Mother<T>::whoAndWhat(); std::cout << "Who : Daughter" << std::endl << "What :" << this->t << std::endl; } int main() { Daughter object; object.t = 123; object.whoAndWhat(); return 0; }