I have the following use case, a struct with some boolean and int variables
struct a { int field1; bool field2; bool field3; };
I am refactoring this code, and writing a constructor for the struct , the problem is the default initialization of fields.
I am not criticizing any language construct here, but ideally I would want null to be part of the language itself
i mean I should be able to define for struct a as
a : field1(null.int), field2(null.bool), field3(null.bool) {}
C++ does not allow it since null.int or null.bool are not defined. Is the only way to do in C++ is
a: field1(-1), field2(false), field3(false) {}
You can do
And all fields will be zero and false respectively. If you want to say that the fields have an indeterminate value, i’m afraid you have to use other techniques. One is to use
boost::optional:Leaves field3 indeterminate. Access the values with
*field_name. Test for a none value withfield == boost::noneorif(field) { ... }.