class 的静态函数可以访问全局静态变量吗?
Can a static function of a class access global static varaibles?
/*In header file */
class abc{
public:
static bool do_something();
}
/*In other file */
static bool isvalid=false; //global variable
bool abc::do_something()
{
return isValid;
}
正在编译fine.I想知道使用是否正确?
是的,它是正确的 - 将没有 isvalid 符号,其他文件将无法看到它。他们可以通过调用 abc::do_something()
来读取它的当前值
成员函数不需要是静态的。所有实例将能够 return 相同的当前值 isValid
在 C++ 中隐藏数据的正常方法是在 class...
中将其设为私有和静态
头文件
class abc{
static bool isValid; // can be seen, does not use space in an instance
public:
static bool do_something();
}
/*In other file */
bool abc::isvalid=false; //global variable
bool abc::do_something()
{
return isValid;
}
/*In header file */
class abc{
public:
static bool do_something();
}
/*In other file */
static bool isvalid=false; //global variable
bool abc::do_something()
{
return isValid;
}
正在编译fine.I想知道使用是否正确?
是的,它是正确的 - 将没有 isvalid 符号,其他文件将无法看到它。他们可以通过调用 abc::do_something()
成员函数不需要是静态的。所有实例将能够 return 相同的当前值 isValid
在 C++ 中隐藏数据的正常方法是在 class...
中将其设为私有和静态头文件
class abc{
static bool isValid; // can be seen, does not use space in an instance
public:
static bool do_something();
}
/*In other file */
bool abc::isvalid=false; //global variable
bool abc::do_something()
{
return isValid;
}