 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Nicola Musatti Guest
|
Posted: Tue Jun 20, 2006 2:44 pm Post subject: Emulating virtual function templates |
|
|
Hallo,
first of all, let me state that I *know* that member function templates
cannot be virtual; however I need to somehow achieve something as close
to the following as possible:
// Beware! Invalid C++
struct base {
template <typename T>
virtual void set(T const & t) = 0;
};
struct env {
template <typename T> void set(T const & t) { p->set(t); }
base * p;
};
What I could come up with is the following:
struct proxy_base {
virtual ~proxy_base() {}
};
template <typename T> struct proxy : proxy_base {
virtual void set(T const &) = 0;
};
struct base2 {
virtual proxy_base * get() = 0;
};
struct env2 {
template <typename T> void set(T const & t) {
proxy_base * pbp = p->get();
assert(dynamic_cast<proxy<T> *>(pbp));
static_cast<proxy<T> *>(pbp)->set(t);
}
base2 * p;
};
Is there any better solution?
Thank you,
Nicola Musatti
[ 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
|
|