简体   繁体   English

静态与非静态变量C ++

[英]Static vs non-static variable C++

class Expression
{
    private:
       ToStringDisplay* toString;
    public:
        ......
};

   // Some code
   if (toString == NULL)
   // do something 

When I debugged, toString referred to an address, not NULL. 当我调试时,toString指向一个地址,而不是NULL。 But if I declared static ToStringDisplay* toString; 但是如果我声明了static ToStringDisplay* toString; to instead, toString referred to NULL ? 改为toString引用NULL? What differ between them ? 它们之间有什么区别?

as a standard in C/C++, static variables are always initialized to null if not stated otherwise. 作为C / C ++中的标准,如果没有另外说明,则静态变量始终初始化为null。 However, for nonstatic local variables there is no such a guarantee 但是,对于非静态局部变量,没有这样的保证

Static member variables are initialized to zero according to http://www.learncpp.com/cpp-tutorial/811-static-member-variables/ where all other are undefined, unless you initialize them explicitly, so they will have whatever random value that was in memory before. 静态成员变量根据http://www.learncpp.com/cpp-tutorial/811-static-member-variables/初始化为零,其中所有其他未定义,除非您明确地对其进行初始化,所以它们将具有任​​意随机值那是以前的记忆 For an explanation on what's the difference with static and non-static see the above link. 有关静态和非静态的区别的解释,请参见上面的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM