M
michael.alexeev
Hi all.
Consider the following code fragment:
#include <string>
struct Base{ virtual ~Base() {} };
template <typename T> struct Derived : public Base {};
Base* Create (int param) {
switch (param) {
case 1:
return new Derived<int>;
case 2:
return new Derived<std::string>;
}
}
void foo(Derived<int>*);
void foo(Derived<std::string>*);
Ideally, I would like to write something like this:
int main() {
Base* base = Create(1);
foo(base);
}
I tried to add downcast method to Base:
template <typename T>
struct Derived;
struct Base{
virtual ~Base() {}
template <typename T>
Derived<T> * downcast(){
Derived<T>* d = static_cast<Derived<T>*>(this);
if (d == 0) throw;
return d;
}
}
But it does not help since I still need to specify Derived type during
foo call:
foo(base->downcast<int>);
which I want to avoid.
Any idea how to do it?
Thanks,
Mike Alexeev
Consider the following code fragment:
#include <string>
struct Base{ virtual ~Base() {} };
template <typename T> struct Derived : public Base {};
Base* Create (int param) {
switch (param) {
case 1:
return new Derived<int>;
case 2:
return new Derived<std::string>;
}
}
void foo(Derived<int>*);
void foo(Derived<std::string>*);
Ideally, I would like to write something like this:
int main() {
Base* base = Create(1);
foo(base);
}
I tried to add downcast method to Base:
template <typename T>
struct Derived;
struct Base{
virtual ~Base() {}
template <typename T>
Derived<T> * downcast(){
Derived<T>* d = static_cast<Derived<T>*>(this);
if (d == 0) throw;
return d;
}
}
But it does not help since I still need to specify Derived type during
foo call:
foo(base->downcast<int>);
which I want to avoid.
Any idea how to do it?
Thanks,
Mike Alexeev