Checking if a variable is initialized

前端 未结 13 1443
后悔当初
后悔当初 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: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 
    class MyClass
    {
        void SomeMethod();
    
        absl::optional mCharacter;
        absl::optional mDecimal;
    };
    
    void MyClass::SomeMethod()
    {
        if (mCharacter)
        {
            // do something with mCharacter.
        }
    
        if (!mDecimal)
        {
            // define mDecimal.
        }
    }
    

提交回复
热议问题