May 11, 2018 12:25 PM
(This post was last modified: May 11, 2018 12:27 PM by chrizator.)
Hey folks, quick question.
Let's assume I have an Enum called 'AnimalType'. And a lot of animals inheriting from a base 'Animal' class (class Dog : public Animal).
Now, based on what 'AnimalType' I have, I want to create the corresponding derivation.
Let's assume (IDK if the shit below compiles, just writing it in HF editor):
I'd like to have a method that does the following:
The code above will probably work, but I find it kind of silly. Especially when working with A LOT of Enum values, this is gonna get ugly.
I'd like to have a method using unordered map or something similiar. I don't know how to achieve that. Still new to C++.
Thanks for taking the time! 🤝
Let's assume I have an Enum called 'AnimalType'. And a lot of animals inheriting from a base 'Animal' class (class Dog : public Animal).
Now, based on what 'AnimalType' I have, I want to create the corresponding derivation.
Let's assume (IDK if the shit below compiles, just writing it in HF editor):
Code
enum class AnimalType
{
DogAnimal,
CatAnimal
}
class Animal {}
class Dog : public Animal {}
class Cat : public Animal {}I'd like to have a method that does the following:
Code
Animal* getAnimalFromEnum(AnimalType type)
{
switch (type)
{
case (AnimalType::DogType):
return new Dog()
break;
case (AnimalType::CatType):
return new Cat()
break;
}
}The code above will probably work, but I find it kind of silly. Especially when working with A LOT of Enum values, this is gonna get ugly.
I'd like to have a method using unordered map or something similiar. I don't know how to achieve that. Still new to C++.
Thanks for taking the time! 🤝

