Checking if a variable is initialized

前端 未结 13 1405
后悔当初
后悔当初 2020-12-28 11:59

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

相关标签:
13条回答
  • 2020-12-28 12:08

    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.

    0 讨论(0)
  • 2020-12-28 12:13

    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.

    0 讨论(0)
  • 2020-12-28 12:15

    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.
        }
    }
    
    0 讨论(0)
  • 2020-12-28 12:16

    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.

    0 讨论(0)
  • 2020-12-28 12:18

    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;
    };
    
    0 讨论(0)
  • 2020-12-28 12:20

    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;
        }
    
    0 讨论(0)
提交回复
热议问题