Reputation: 118
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 ?
Upvotes: 8
Views: 385
Reputation: 33864
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.
Upvotes: 7
Reputation: 1658
If you are not hesitant to use the boost library, then maybe you can do it using boost::optional
.
#include <boost/optional.hpp>
enum Color {
red,
blue
};
class example
{
boost::optional<Color> c;
void set_color(Color c) { this->c = c; }
Color get_color() { return c; }
bool check_color_is_set()
{
if( c ){
// color is set
return true;
}
else
{
// color is not set.
return false;
}
}
}
The default value of boost::optional<Color> c
will be boost::none
by default. You can perform normal if-checks on it and see if something has been set it or not.
Here is the documentation of Boost Optional for your reference.
NOTE : I haven't used the STL version of optional but if you are not interested in using Boost, you can always use std::optional
as well.
Upvotes: 2
Reputation: 1701
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;
}
Upvotes: 4