我在我的陈述中缺少任何关于c ++的东西? [英] Am I missing anything here in my statement about c++?
问题描述
除了声明,定义和预处理器指令之外,您不能有函数之外的代码。
You can't have code outside of functions except for declarations, definitions and preprocessor directives.
这个语句是否准确,我在教我的侄子去编程,他正试图在主循环之前放一个循环。
Is that statement accurate, or is there something I'm missing? I'm teaching my nephew to program, and he was trying to put a while loop before main. He's pretty young, I want to give him a hard simple rule that he can understand.
推荐答案
不完全 - 你也可以put全局变量声明中的表达式:
Not quite -- you can also put expressions in global variable declarations:
int myGlobalVar = 3 + SomeFunction(4) - anotherGlobalVar;
但您只能在此处放置 您正在初始化全局的值。你不能把完整的语句(没有代码块,如果
语句,没有循环等)。这个代码将在 main()
之前执行,有机会运行,所以要小心你在这里做的。我建议不要调用全局初始化器中的函数,除非你不能避免它。
But you can only put expressions here, which have to evaluate to the value you're initializing the global with. You cannot put full statements (no blocks of code, no if
statements, no loops, etc.). This code will get executed before main()
gets a chance to run, so be careful with what you do here. I'd recommend against calling functions in global initializers unless you can't avoid it.
这篇关于我在我的陈述中缺少任何关于c ++的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!