I was reading the sample code on another post Specializations only for C++ template function with enum non-type template parameter
and I'm trying to take it one step further, by using a overloaded conversion operator to use the object as if it was its enum member to call a template function.
//in my .h
enum class AllowedTypes { Cat, Dog };
class A {
AllowedTypes animal;
public:
//constructor
A(AllowedTypes t): animal(t) {};
explicit operator AllowedTypes*() const { return (AllowedTypes*) animal; }
operator AllowedTypes() const { return animal; }
template <AllowedTypes type>
void ability() const;
}
//second class
struct B{
//tempalte function
template <AllowedTypes type>
void ability() const;
}
//in my cpp
template<>
void B::ability<AllowedTypes::Dog>() const
{
std::cout << "Dog ability." << std::endl;
}
template<>
void B::ability<AllowedTypes::Cat>() const
{
std::cout << "Cat ability." << std::endl;
}
//in my main
Class* a = new A(AllowedType(1))
Class* b = new B();
//this calls work!
b->ability<AllowedTypes::Cat>();
//trying to get the type and calling ability via conversion doesn't
AllowedTypes type = (AllowedTypes)*a; // this converts correctly!
b->ability<type>(); //does not call correctly
this last line doesn't work, I'm trying to put my converted object in the template and call the ability specific to the type that A has. I've tried a few different things but can't seem to find what I'm looking for, is there a proper way to do this?