EDIT:
I realized that this code compiles and works:
#include <iostream>
template<class Something> class Base {
public:
Base() {
}
virtual ~Base() {
}
virtual void method() = 0;
};
class Derived: public Base<std::string> {
public:
Derived() {
}
virtual ~Derived() {
}
void method() {
}
};
template<class Something> class TemplatedClass {
public:
void method(Base<Something>* base) {
}
};
class SomeClass: public TemplatedClass<std::string> {
};
int main(int argc, char **argv) {
Derived* d = new Derived();
Base<std::string>* b = d;
Base<std::string>* b1 = dynamic_cast<Base<std::string>*>(d);
Base<std::string>* b2 = static_cast<Base<std::string>*>(d);
SomeClass sc;
sc.method(b);
sc.method(b1);
sc.method(b2);
}
Then, I am doing a really big mistake preparing the skeleton or even trying to understand my own code. WHAT a SHAME!!! Sorry… This is a really bad question. It would be better to remove it…
** NO SENSE QUESTION AND CODE BELOW **
Given this:
template<class Something> class Base
{
virtual void method() = 0;
};
class Derived: public Base<std::string>
{
void method() { ... };
};
I want to downcast a pointer of Derived to a pointer of Base. I tried to use dynamic_cast with no success:
...
Derived* d = new Derived();
Base* b = dynamic_cast<Base*>(d);
...
I always got errors during execution.
I don’t know if I need downcasting,. I have this situation:
template<class Something> class TemplatedClass
{
void method(Base<Something>* base) { ... };
};
class SomeClass : public TemplatedClass<std::string> { ... };
And I want to call (EDITED*):
Derived* d = new Derived();
SomeClass sc;
sc.method(d);
But it gives me an conversion error.
What does I need? Downcasting or what?
Presumably your test actually looked like this?
i.e. it has to be a cast to the instantiated template, not
Basewithout a type argument.Wait a second – you’re doing derived to base!
That should be fine!
Your second example doesn’t look right – in particular:
That should be:
Can you post the real code and the exact error message?