Hello,
I'd like to specialize a method in a template class for another generic template class and can't figure out how to do it exactly.
The simplified code looks like this:
template<typename T>
class ClassA
{
public:
ClassA(){...}
~ClassA(void){...}
ClassA(const ClassA<T> &other){...}
ClassA<T>& operator=(const ClassA<T> &other){...}
inline T& operator() (int height, int width){
if (height < 0 || height >= getHeight() || width < 0 || width >= getWidth())
...
else
return _pMat[height][width];
}
inline T operator() (int height, int width) const{
if (height < 0 || height >= getHeight() || width < 0 || width >= getWidth())
...
else
return _pMat[height][width];
}
inline int getWidth(void){return _iWidth;}
inline int getWidth(void) const{return _iWidth;}
inline int getHeight(void){return _iHeight;}
inline int getHeight(void) const{return _iHeight;}
void print(void){
std::cout << "print general" << std::endl;
}
private:
int _iWidth;
int _iHeight;
T** _pMat;
};
template<>
void ClassA< ClassB<int> >::print(void){
std::cout << "print specialized <int>" << std::endl;
}
template <typename U>
class ClassB
{
public:
inline ClassB(){...}
inline ~ClassB(void){...}
inline ClassB(const ClassB<U> &other){...}
inline ClassB<U>& operator=(const ClassB<U> &other){...}
inline U& operator() (int dimension){
if (dimension >= 0 && dimension < getDimension())
return _pData[dimension];
else
...
}
inline U operator() (int dimension) const{
if (dimension >= 0 && dimension < getDimension())
return _pData[dimension];
else
...
}
inline int getDimension(void) const{return _iDimension;}
inline int getDimension(void){return _iDimension;}
inline void print(void){...}
private:
int _iDimension;
U *_pData;
};
template<typename T>
class ClassA
{
public:
ClassA(){...}
~ClassA(void){...}
ClassA(const ClassA<T> &other){...}
ClassA<T>& operator=(const ClassA<T> &other){...}
inline T& operator() (int height, int width){
if (height < 0 || height >= getHeight() || width < 0 || width >= getWidth())
...
else
return _pMat[height][width];
}
inline T operator() (int height, int width) const{
if (height < 0 || height >= getHeight() || width < 0 || width >= getWidth())
...
else
return _pMat[height][width];
}
inline int getWidth(void){return _iWidth;}
inline int getWidth(void) const{return _iWidth;}
inline int getHeight(void){return _iHeight;}
inline int getHeight(void) const{return _iHeight;}
void print(void){
std::cout << "print general" << std::endl;
}
private:
int _iWidth;
int _iHeight;
T** _pMat;
};
template<>
void ClassA< ClassB<int> >::print(void){
std::cout << "print specialized <int>" << std::endl;
}
template <typename U>
class ClassB
{
public:
inline ClassB(){...}
inline ~ClassB(void){...}
inline ClassB(const ClassB<U> &other){...}
inline ClassB<U>& operator=(const ClassB<U> &other){...}
inline U& operator() (int dimension){
if (dimension >= 0 && dimension < getDimension())
return _pData[dimension];
else
...
}
inline U operator() (int dimension) const{
if (dimension >= 0 && dimension < getDimension())
return _pData[dimension];
else
...
}
inline int getDimension(void) const{return _iDimension;}
inline int getDimension(void){return _iDimension;}
inline void print(void){...}
private:
int _iDimension;
U *_pData;
};
To copy to clipboard, switch view to plain text mode
if I instantiate the templates like this:
ClassA< ClassB<int> > test;
test.print();
ClassA< ClassB<int> > test;
test.print();
To copy to clipboard, switch view to plain text mode
everything works as desired and the specialized print method is called when ClassB is of type <int>.
but I also need specialized methods for:
ClassA< ClassB<double> > test1;
ClassA< ClassB<float> > test2;
//etc...
ClassA< ClassB<double> > test1;
ClassA< ClassB<float> > test2;
//etc...
To copy to clipboard, switch view to plain text mode
so I'd like to spezialize my print method like this:
template<>
void ClassA< ClassB<U> >::print(void){
std::cout << "print specialized <U>" << std::endl;
}
template<>
void ClassA< ClassB<U> >::print(void){
std::cout << "print specialized <U>" << std::endl;
}
To copy to clipboard, switch view to plain text mode
I hope this is possible, and I think I'm missing some template syntax to make it work.
I hope someone knows how to do it...
thanks in advance
darksaga
Bookmarks