我们可以通过在类之外用“点"定义一个类变量来制作全局变量吗?操作员? [英] can we make global variables by defining a class variable outside of the class with "dot" operator?
本文介绍了我们可以通过在类之外用“点"定义一个类变量来制作全局变量吗?操作员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class abc
{
public:
int x;
};
abc b1;
b1.x=10;
int main()
{
}
为什么我们不能在main函数之外写这个b1.x=10;
?如果我们在 main 函数之外写 b1.x=10;
会报错,为什么?
why can't we write this b1.x=10;
outside of the main function?
it shows error if we write b1.x=10;
outside of the main function, why?
推荐答案
因为,b1.x=10;
是一个赋值语句,不能出现在文件范围.
Because, b1.x=10;
is an assignment statement which cannot be present at file scope.
但是,您可以使用初始化来提供初始值.类似的东西
You can, however, use initialzation to provide the initial values. Something like
abc b1 = {10};
这篇关于我们可以通过在类之外用“点"定义一个类变量来制作全局变量吗?操作员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文