Undefined / Uninitialized default values in a class

Posted by Jir on Stack Overflow See other posts from Stack Overflow or by Jir
Published on 2010-06-01T09:32:17Z Indexed on 2010/06/01 9:43 UTC
Read the original article Hit count: 197

Filed under:
|
|

Let's suppose you have this class:

class A
{
public:
  A () {}
  A (double val) : m_val(val) {}
  ~A () {}
private:
  double m_val;
};

Once I create an instance of A, how can I check if m_val has been initialized/defined? Put it in other words, is there a way to know if m_val has been initialized/defined or not? Something along the lines of the defined operator in Python, I suppose. (But correct me if I'm wrong.)

I thought of modifying the class and the c-tors the following way:

class A
{
public:
  A () : defined(false) {}
  A (double val) : m_val(val), defined(true) {}
  ~A () {}
private:
  double m_val;
  bool defined;
};

How do you rate this solution? Any suggestion?

TIA, Chris

© Stack Overflow or respective owner

Related posts about c++

Related posts about initialization