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.
[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.
Comment