c++ - Using Templates to resolve virtual methods -


this issue involves using templates resolve virtual members in dispatch pattern.
note: not same virtual template method questions asked on stackoverflow. *

edit 1: corrected syntax errors, added clarifications.

given following:

#include <string> #include <iostream>  class field_interface {   public:     virtual std::string  get_field_name(void) const = 0; };  class field_integer : public field_interface {   public:     std::string get_field_name(void) const     { return "int";} };  class field_string : public field_interface {   public:     std::string get_field_name(void) const     { return "varchar";} };  class field_double : public field_interface {   public:     std::string get_field_name(void) const     { return "double";} };   class abstract_visitor {   public:   virtual void visit(const field_integer& fi) = 0;   virtual void visit(const field_string& fi) = 0;   virtual void visit(const field_double& fi) = 0; };  class visitor_name_query_1 : public abstract_visitor {   public:   template <class field>   void visit(const field& f)   {       std::cout << "field name is: "                 << f.get_field_name()                 << "\n";   } };  class visitor_name_query_2 : public abstract_visitor {   public:     void visit(const field_integer& fi)     { print_field_name(fi); }      void visit(const field_string& fi)     { print_field_name(fi); }      void visit(const field_double& fi)     { print_field_name(fi); }    private:     void print_field_name(const field_interface& fi)     {          std::cout << "field name is: "                   << fi.get_field_name()                   << "\n";     } };  int main(void) {     visitor_name_query_1    q1;     field_integer           fi;     q1.visit(f1);     return 0; } 

the compiler saying templated method in visitor_name_query_1 not resolving abstract interface abstract_visitor.

edit 2: results g++

# g++ -o main.exe main.cpp main.cpp: in function `int main()': main.cpp:75: error: cannot declare variable `q1' of type `visitor_name_query_1' main.cpp:75: error:   because following virtual functions abstract: main.cpp:35: error:  virtual void abstract_visitor::visit(const field_integer&) main.cpp:36: error:  virtual void abstract_visitor::visit(const field_string&) main.cpp:37: error:  virtual void abstract_visitor::visit(const field_double&) main.cpp:77: error: `f1' undeclared (first use function) main.cpp:77: error: (each undeclared identifier reported once each function appears in.) 

visitor_name_query_1 attempt simplify class visitor_name_query_2. when number of visit methods grows beyond simple quantity (like 5), maintenance becomes tedious. reason template declaration.

when template expanded, 1 of field types, declaration matches 1 in visitor_name_query_2.

so why compiler generating saying class visitor_name_query_1 abstract?

note: using visual studio 2008 on windows vista.

* other posts involve using templates create virtual method declarations. i'm using templates create functions implement abstract methods.

so why compiler generating saying class visitor_name_query_1 abstract?

because standard says so. §14.5.2 [temp.mem]/p4:

a specialization of member function template not override virtual function base class. [ example:

class b {     virtual void f(int); }; class d : public b {     template <class t> void f(t); // not override b::f(int)     void f(int i) { f<>(i); }     // overriding function calls                                   // template instantiation }; 

end example ]


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

python - Django-cities exits with "killed" -

python - How to get a widget position inside it's layout in Kivy? -