Search code examples
templatescastingstatic-caststatic-polymorphism

C++: downcasting and upcasting between derived template class and base class?


I am having trouble converting a pointer to a templated derived class to a base class pointer for the purposes of storing in a map(and obviously retrieving it later.). I have:

#include <map>
//Role.h
class RoleBase{};
enum class RoleEnum : int;

template<RoleEnum role>
class Role : public RoleBase{};

//Relationship.h
class Relationship{
public:
    template<RoleEnum role>
    Role<role>* getRole(){
    auto it=RoleMap.find(role);
    if ( it == RoleMap.end() ) {
       return nullptr;
    } else {
            RoleBase* roleBase= it->second;
            return static_cast<Role<role>* >(roleBase);
        }
    }
    std::map<RoleEnum,RoleBase*> RoleMap;
};

//squash.h
enum class RoleEnum : int
{
    Player,
    Referee
};

template<> class Role<RoleEnum::Player>{};
template<> class Role<RoleEnum::Referee>{};

class SquashGame: public Relationship{
public:
    SquashGame(){
        RoleBase* playerRole=new Role<RoleEnum::Player>; //problematic
        RoleMap.emplace(RoleEnum::Player,playerRole);
    }
};

int main() {
    SquashGame squashGame;
    squashGame.getRole<RoleEnum::Player>();
    return 0;
}

Why is that and is there a way of fixing that so I can template a class with enum values for the purposes of calling externally by a getClass<Enum> function, as is hopefully clear in the example?


Solution

  • The problem is simple: your redefinition of Role

    template<> class Role<RoleEnum::Player> {}
    

    Doesn't extend RoleBase.

    Either remove it or change it to:

    template<> class Role<RoleEnum::Player> : public RoleBase {}