0

I read in data from an XML file, depending on the tags in the xml file, data gets attached to class member variables.

Is it possible if for example a value in the xml file contains "!", which in this case is not valid thus I can't accept that value. So the member variable for that value is empty.

But the type of some member variables are other classes or integers or boolean. How can I check if those values are set? As there is no function emtpy() for those.

fduff
  • 3,671
  • 2
  • 30
  • 39
user1008531
  • 491
  • 2
  • 8
  • 17
  • 1
    sorry - without sample code & the parser that you are using, nobody can help you. – Tobias Langner Aug 16 '12 at 11:41
  • 1
    Provide some code becuase your description is confusing at best and impossible to offer constructive assistance based upon. – Graeme Aug 16 '12 at 11:52
  • Disagree with downvotes. The only problem in the question is really the terminology. We don't "attach" data to member variables; we _assign_ it, or we _initialize_ them. And it's perfectly OK to ask how we can check whether an `int` is initialized. – MSalters Aug 16 '12 at 13:06

3 Answers3

2

If they are not optional, you must cause your parsing mechanism to error when they are not present. Else, you could use something like boost::optional.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

you could during XML read, check the XML value and if it contains "!", assign a default value to whatever variable it is.

e.g. set ptr to nullptr, boolean to false and int to 0 or -1.

Use const default values whenever you can, that will make your code clearer and easier to maintain.

fduff
  • 3,671
  • 2
  • 30
  • 39
  • The question is tagged C++, so you can and should use a `const` variable instead of a `#define`. E.g. Don't use `NULL` but `nullptr`. – MSalters Aug 16 '12 at 13:08
0

There is no way to detect at run time, whether a variable has been explicitly set. That's why some compilers give you a warning (not an error), if they suspect that a variable might be used uninitialized.

It is the programmer's responsibility, to keep track of what variables have been set. The low level way to do this, is to use pointers, initialize them to 0, change them when they should point to some initialized memory and change them back to 0 when the object they point to is deleted.

In C++, you can use Boost.Optional to spare you from messing around with pointers in this way.

Oswald
  • 31,254
  • 3
  • 43
  • 68