I want to check if a variable in a 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 if the variable Color c
was assigned or not ?
In C#, we can check the specific type is enum or not by using the IsEnum property of the Type class. It will return true if the type is enum. Otherwise, this property will return false. It is a read-only property.
Every enum constant is always implicitly public static final. Since it is static, we can access it by using the enum Name. Since it is final, we can't create child enums. We can declare the main() method inside the enum.
Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.
An enumeration type declaration gives the name of the (optional) enumeration tag. And, it defines the set of named integer identifiers (called the enumeration set, enumerator constants, enumerators, or members). A variable of the enumeration type stores one of the values of the enumeration set defined by that type.
Other than having a value for not assigned (as in @code707's answer) you can do a few things.
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.
...
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;
}
3 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.
What about having special value for "not assigned" ?
enum Color {
nocolor,
red,
blue
};
class example {
Color c = nocolor;
void set_color(Color c) { this->c = c; }
Color get_color() { return c; }
bool check_color_is_set() {
if (c == nocolor) {}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With