I'm trying to write an operator method that will convert from the class member's type (which is templated itself) to another type. I've got my simplified class and the method declared and defined as follows:
1 2 3 4 5 6 7 8 9
template <typename A> class Classname
{
A *ptr;
public:
template <typename B> operator B* ();
};
template <typename A, typename B> Classname<A>::operator B* ()
{ return (*B)ptr; }
What's wrong with this code? Do I need to use a different cast operator?
{ return (*B)ptr; } // wrong
{ return (B*)ptr; } // you probably wanted this
But really, I have to question the overall wisdom of this. You can't just cast any type to any other type. Doing this is very dangerous. You're basically side-stepping the entire type safety system in the language.