 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthieu Paindavoine Guest
|
Posted: Wed Aug 27, 2003 9:39 am Post subject: virtual member template functions workaround |
|
|
Hello,
I am coding a < composite / visitor / combinator > sandwich, and I
bumped again what seems a tough cookie: virtual member template
functions are not allowed.
#include <iostream>
class A
{
public:
template < class T >
void Op(T& _t)
{
std::cout << "A::Op " << _t.vis() << std::endl;
}
virtual void V_Op()
{
std::cout << "A::V_Op" << std::endl;
}
};
class B : public A
{
public:
template < class T >
void Op(T& _t)
{
std::cout << "B::Op " << _t.vis() << std::endl;
}
virtual void V_Op()
{
std::cout << "B::V_Op" << std::endl;
}
};
class X
{
public:
void vis()
{
std::cout << "X::vis" << std::endl;
}
};
int main()
{
A* a = new A();
A* b = new B();
X x;
a->Op(x);
a->V_Op();
b->Op(x);
b->V_Op();
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
So I would love to stick a virtual in front of the void Op(T& _t), but
I can't
Is there a workaround, or this is dead-end an indication I took a
wrong term some time ago?
Thank you for your suggestions
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Sat Aug 30, 2003 11:25 pm Post subject: Re: virtual member template functions workaround |
|
|
Matthieu Paindavoine wrote:
| Quote: | Hello,
I am coding a < composite / visitor / combinator > sandwich, and I
bumped again what seems a tough cookie: virtual member template
functions are not allowed.
#include <iostream
class A
{
public:
template < class T
void Op(T& _t)
{
std::cout << "A::Op " << _t.vis() << std::endl;
}
virtual void V_Op()
{
std::cout << "A::V_Op" << std::endl;
}
};
class B : public A
{
public:
template < class T
void Op(T& _t)
{
std::cout << "B::Op " << _t.vis() << std::endl;
}
virtual void V_Op()
{
std::cout << "B::V_Op" << std::endl;
}
};
class X
{
public:
void vis()
{
std::cout << "X::vis" << std::endl;
}
};
int main()
{
A* a = new A();
A* b = new B();
X x;
a->Op(x);
a->V_Op();
b->Op(x);
b->V_Op();
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
So I would love to stick a virtual in front of the void Op(T& _t), but
I can't
Is there a workaround, or this is dead-end an indication I took a
wrong term some time ago?
|
It's not really clear to me what you want to achieve with this but the
obvious solution to me is to factor out the code that is common to all
types T.
class A
{
public:
template <class T>
void Op(T& _t)
{
std::cout<
}
private:
virtual const char* common_code() const
{
return "A::Op ";
}
};
Last week on de.comp.lang.iso-c++ a similar problem arose and we found a
solution for a fixed number of types T by not making the virtual
function a template and to generate the class with a type list and a
class that describe the function body in a static function.
regards
Torsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|