Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;返回全局常量变量的静态方法_C++_Global Variables_Constants_Static Methods - Fatal编程技术网

C++ C++;返回全局常量变量的静态方法

C++ C++;返回全局常量变量的静态方法,c++,global-variables,constants,static-methods,C++,Global Variables,Constants,Static Methods,用C++编写以下内容是否安全: // AClass.h class AClass { public: static int get_max_size(); }; // AClass.cpp namespace { const int MAX_SIZE = 123; } int AClass::get_max_size() { return MAX_SIZE; } 是的,你担心什么?不明确的行为,泄露,等等。现在在这个例子中,变量是一个普通的int,但是它

用C++编写以下内容是否安全:

// AClass.h

class AClass
{
public:
    static int get_max_size();    
};

// AClass.cpp

namespace
{
    const int MAX_SIZE = 123;
}

int AClass::get_max_size()
{
    return MAX_SIZE;
}

是的,你担心什么?不明确的行为,泄露,等等。现在在这个例子中,变量是一个普通的
int
,但是它通常也是安全的吗?我明白了。这取决于用例。这个特定的例子很好,但是如果您返回一个指向卸载模块中全局对象的指针,情况就会发生变化。指出您认为不安全的地方。这是非常开放的。这个问题如何被未来的访问者发现并帮助他们?这取决于函数何时被调用。