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 init
There's no reasonable way to check whether a value has been initialized.
If you care about whether something has been initialized, instead of trying to check for it, put code into the constructor(s) to ensure that they are always initialized and be done with it.
Depending on your applications (and especially if you're already using boost), you might want to look into boost::optional.
(UPDATE: As of C++17, optional is now part of the standard library, as std::optional)
It has the property you are looking for, tracking whether the slot actually holds a value or not. By default it is constructed to not hold a value and evaluate to false, but if it evaluates to true you are allowed to dereference it and get the wrapped value.
class MyClass
{
void SomeMethod();
optional<char> mCharacter;
optional<double> mDecimal;
};
void MyClass::SomeMethod()
{
if ( mCharacter )
{
// do something with *mCharacter.
// (note you must use the dereference operator)
}
if ( ! mDecimal )
{
// call mDecimal.reset(expression)
// (this is how you assign an optional)
}
}
More examples are in the Boost documentation.
If you donot like boost and c++17, the google c++ lib Abseil is another method to realize that. absl::optional is just like std::optional.
#include <absl/types/optional.h>
class MyClass
{
void SomeMethod();
absl::optional<char> mCharacter;
absl::optional<double> mDecimal;
};
void MyClass::SomeMethod()
{
if (mCharacter)
{
// do something with mCharacter.
}
if (!mDecimal)
{
// define mDecimal.
}
}
There is no way in the C++ language to check whether a variable is initialized or not (although class types with constructors will be initialized automatically).
Instead, what you need to do is provide constructor(s) that initialize your class to a valid state. Static code checkers (and possibly some compilers) can help you find missing variables in constructors. This way you don't have to worry about being in a bogus state and the if
checks in your method can go away completely.
By default, no you can't know if a variable (or pointer) has or hasn't been initialized. However, since everyone else is telling you the "easy" or "normal" approach, I'll give you something else to think about. Here's how you could keep track of something like that (no, I personally would never do this, but perhaps you have different needs than me).
class MyVeryCoolInteger
{
public:
MyVeryCoolInteger() : m_initialized(false) {}
MyVeryCoolInteger& operator=(const int integer)
{
m_initialized = true;
m_int = integer;
return *this;
}
int value()
{
return m_int;
}
bool isInitialized()
{
return m_initialized;
}
private:
int m_int;
bool m_initialized;
};
If for instance you use strings instead of chars, you might be able do something like this:
//a is a string of length 1
string a;
//b is the char in which we'll put the char stored in a
char b;
bool isInitialized(){
if(a.length() != NULL){
b = a[0];
return true;
}else return false;
}