I am trying switch case and inheritance in C++ and found some problems/ warnings.
For example I have an abstract basic class Field:
Field.h class Field{ private: FieldType type_; public: enum FieldType { GRASS,WATER,STREET,HOME,TOWNHALL }; virtual bool checkIsBuildable(Fieldtype type); Now I get warnings in subclasses Buildings.cpp and Properties.cpp:
warning enumeration value GRASS,WATER,STREET bit handled in switch Since it is a bool i just can return false or true in default and the method wont work propely or? I just want to check for example Home and Townhall in Buildings.cpp and Grass, Water and street in Properties.
Buildings.cpp bool Buildings::isBuildable(Field::FieldType type) { switch(type) { case Field::HOME: return true; case Field::TOWNHALL: return false; } } Properties.cpp bool Properties::isBuildable(Field::FieldType type) { switch(type) { case Field::GRASS: return true; case Field::WATER: return false; case Field::STREET: return false; } }
switch? What do you return then? Add adefaultcase.case X: caseY: commonXYCode(); break;switchdoesn't cover all the values in yourenum. The warning is suggesting to add adefaultcase so it is clear that all other cases not explicitly mentioned are handled.defaultcase, because the caller already should have arranged for that, then athrow std::invalid_argumentmakes sense for the default case.switchstatement.return type == HOME || type == TOWNHALL;.