template virtual function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alind
    New Member
    • Sep 2007
    • 7

    template virtual function

    I m having a problem with templates. A per C++ std says i cant use override template virtual functions. in other words virtual functions cant be templates due to some vtable size issues. Now i want to simulate it somehow. I read in some other posts that it is somehow possible throg visitor pattern but i m unable to get it done. My intended code is posted below. can somebody give me a workaround to get this functionality.A ny help is appreciated. I tried searching in other posts but was not able to get the correct ans to my problem. Thanx in advance.
    [CODE=cpp]//--------------------------
    #include <iostream>
    using namespace std;

    class Mat
    {
    public:
    template <class T> virtual void read(T &x) = 0;
    template <class T>virtual void read(T &x, const int &y) = 0;
    };
    class Mat1:public Mat
    {
    public:
    /*virtual*/ template <class T> void read(T &x)
    {
    cout << "mat1 ::x"<< zz <<endl;
    }
    /*virtual*/ template <class T> void read(T &x, const int &y)
    {
    cout << "mat1 ::xx"<< zz <<endl;
    }
    private:
    int zz;
    };
    class Mat2:public Mat
    {
    public:
    /*virtual*/ template <class T> void read(T &x)
    {
    cout << "mat2 ::x"<<z << endl;
    }
    /*virtual*/ template <class T> void read(T &x, const int &y)
    {
    cout << "mat2 ::xx "<< z <<endl;
    }
    private:
    int z;
    };

    int main()
    {
    Mat* x = new Mat1();
    x->read(3);
    x->read (4,2);
    return 0;
    }
    //--------------------------[/CODE]

    //passing of the variables in functions are importatnt coz the internal //functionality depends on them. Also private members are also needed //in my design.
    Last edited by alind; Sep 10 '07, 07:13 AM. Reason: some simple typo errors
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    ::bumping because I was never good at template classes :( ::

    Comment

    • alind
      New Member
      • Sep 2007
      • 7

      #3
      plz help me with the problem ?
      :(

      Comment

      • Benny the Guard
        New Member
        • Jun 2007
        • 92

        #4
        One thing you could try is make your class the template. Then no problems overriding the virtual functions. Should work as long as you have T the same for every function.

        Comment

        • alind
          New Member
          • Sep 2007
          • 7

          #5
          i cannot make the class as template because in my case T is the thing that is going to vary. (i want to use boost matrices as T.) THe selection of pasticular type of boost matrix will depend upon the tun time conditions.

          Comment

          • alind
            New Member
            • Sep 2007
            • 7

            #6
            so it seems that my problem does not have any solution
            k thanks for aa the help buddies.

            Comment

            Working...