Seems like this would be a duplicate, but maybe it is just so obvious it hasn’t been asked…
Is this the proper way of checking if a variable (not pointer) is initialized in a C++ class?
class MyClass
{
void SomeMethod();
char mCharacter;
double mDecimal;
};
void MyClass::SomeMethod()
{
if ( mCharacter )
{
// do something with mCharacter.
}
if ( ! mDecimal )
{
// define mDecimal.
}
}
There is no way of checking of the contents of a variable are undefined or not. The best thing you can do is to assign a signal/sentinel value (for example in the constructor) to indicate that further initialization will need to be carried out.