检查变量是否已初始化 [英] Checking if a variable is initialized

查看:116
本文介绍了检查变量是否已初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎这将是一个重复,但也许它是很明显,它没有被问...

Seems like this would be a duplicate, but maybe it is just so obvious it hasn't been asked...

这是正确的方式检查在C ++类中初始化一个变量(不是指针)?

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.
    }
}


推荐答案

是没有办法检查变量的内容是否未定义。你可以做的最好的事情是分配一个signal / sentinel值(例如在构造函数中),以指示需要进一步初始化。

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.

这篇关于检查变量是否已初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆