I have a generic class Client
which has a single type parameter. The type parameter is any reference type derived from Base
. So I declare the class as below:
generic <class T>
where T : Base, gcnew()
public ref class Client {
T val;
void SetVal(Base ^bval){
val = dynamic_cast<T>(bval); // error C2682: cannot use 'dynamic_cast' to convert from 'Base ^' to 'T'
}
};
Why do I get a compile error when using dynamic_cast
to convert from Base
to one of its derived classes? I was expecting the cast to compile but fail at runtime (by returning nullptr
) in case the argument was of incorrect type.
Try dynamic_cast <T^>
.
Pointers have to be cast to other pointers, handles to other handles, and so forth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With