Can someone explain what is going on inside the struct function? I'm really confuse why MonsterType Type; is written likes this? It's not declare anything I don't think. This solution is not mine it's a solution from another site that I found, I'm trying to understand what is going on in the code.None of these are not my comments either it came with the solution the only one that is mine is the one I'm asking a question on that has the two brackets. Thanks
#include <iostream>
// Define our different monster types as an enum
enumclass MonsterType
{
OGRE,
DRAGON,
ORC,
GIANT_SPIDER,
SLIME
};
// Our monster struct represents a single monster
struct Monster
{
MonsterType type; // {{<---- What is the line of code doing? }}
std::string name;
int health;
};
// Return the name of the monster's type as a string
// Since this could be used elsewhere, it's better to make this its own function
std::string getMonsterTypeString(Monster monster)
{
if (monster.type == MonsterType::OGRE)
return"Ogre";
if (monster.type == MonsterType::DRAGON)
return"Dragon";
if (monster.type == MonsterType::ORC)
return"Orc";
if (monster.type == MonsterType::GIANT_SPIDER)
return"Giant Spider";
if (monster.type == MonsterType::SLIME)
return"Slime";
return"Unknown";
}
// Print our monster's stats
void printMonster(Monster monster)
{
std::cout << "This " << getMonsterTypeString(monster);
std::cout << " is named " << monster.name << " and has " << monster.health << " health.\n";
}
int main()
{
Monster ogre = { MonsterType::OGRE, "Torg", 145 };
Monster slime = { MonsterType::SLIME, "Blurp", 23 };
printMonster(ogre);
printMonster(slime);
return 0;
Okay so are you saying in this line of code MonsterType type; inside the struct, what it's doing it's passing the enum class called MonsterType into the field of the structure that is declare to the variable type? Right?