【发布时间】:2021-10-11 22:27:06
【问题描述】:
我正在处理一个大型项目,需要将一个作用域变量设为全局变量,以便整个班级都可以访问它。
可能使用它的一个简单场景是使整数x 全局化。
class a
{
public:
a()
{
int x;
}
void print()
{
std::cout << x << std::endl;
}
}
更复杂的场景可能包括制作FunctionTypeClass<FunctionType> FunctionTypeDataStore:
class thread
{
public:
template<typename FunctionType>
thread(FunctionType* function)
{
FunctionTypeClass<FunctionType> FunctionTypeDataStore;
}
FunctionTypeClass get_function_type()
{
return FunctionTypeDataStore;
}
~thread()
{
}
template<typename StoreFunctionTypeTemp>
class FunctionTypeClass
{
StoreFunctionTypeTemp Variable;
}
}
【问题讨论】:
-
我认为你应该先弄清楚成员变量是什么,然后再开始将变量设为全局
-
"所以整个班级都可以访问它。"那是一个成员变量,你不需要一个全局变量
-
你不能让它成为类本身的数据成员吗?
class a{ intx; }?
标签: c++ variables global-variables scoped