How to check a defined enum type varaible in class was assigned or not?

  • A+
Category:Languages

I want to check the variable in class was set or not? How can I do it?

enum Color {    red,    blue }; class example {    Color c;    void set_color(Color c) { this->c = c; }    Color get_color() { return c; }    bool check_color_is_set() {        if (c == Color) {} // compile error        if (c == NULL) {} // compile error        return true;    } 

I want to know the c was assigned or not ?

 


Other than having a value for not assigned (as in @code707's answer) you can do a few things.

Same approach, user can use the class uninitialized - mitigation

All are based on storing a bool if the user calls set correctly.

Use your get/set encapsulation to check if it has been set:

class example { private:     Color color;     bool isColourSet = false;  public:     Color get_color() const;      void set_color(Color newCol) {        color = newCol;        isColourSet = true;     } ... 

Then you can use this bool in various ways depending on your desire:


Throw an exception

    Color get_color() const {        if (!isColourSet) throw some_exception; // It wasnt Set!! Throw        return color;     } 

This way, you don't have an extra enumeration element, and you protect against dishing out an undefined value by throwing an exception.


Returning an error code

If you don't want to throw an exception, you could pass a return argument and return an error:

ErrInt get_colour(Colour &out) const {     if (!isColourSet) return STANDARD_ERROR_VAL;     out = color;     return STANDARD_SUCCESS_VALUE; } 

This is quite similar to the concept of a Bad Value in the enumeration.


Use std::optional

Since c++17 you also have a new way std::optional, where you can optionaly return (instead of throwing an exception or returning an error value).

class example { public:     std::optional<Color> get_colour() {         return color;     }     void set_color(Color newColor) {         color = newColor;     } private:     std::optional<Color> color; } 

Then the user could use it as such:

auto myColor = myExample.get_colour(); if (myColour) { // Here you can check the optional     myColour.get() // Do something with colour. ... 

New approach, user cannot set the class in a bad state - redesign

As opposed to setting a value and having to check if it has been set correctly, perhaps you can design the class such that it can never be in an invalid state. Lets walk through how we might do this:

1 Type Safety

We don't want a user to be able to set a random value for color that we dont know about. In the current implementation enum will be a type that can be set with likely any int value! What does it mean when someone does set_color(538)? We can fix this with c++11 enum class:

enum class Color {     RED,     BLUE }  // This now causes a compiler error: set_color(523); // You can only use it like this: set_color(Color::Red); 

2 Constructor Arguments

We can force the user to choose the initial colour by providing only a constructor that requires a color:

class example { public:     example(Color startColor) : color(startColor) {}         ... private:     Color c; } 

This means that a user will be unable to create the class without it having an initial color. Now we can add our set function in case the user wants to change the color:

void change_color(Color newColor) {     color = newColor; } 

Conclusion

Now you have a class where the user can never get themselves into a situation where it is invalid. I believe this is much better then trying to detect if the user has stuffed up creating the class.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: