C ++可以在全局范围内使用代码吗? [英] Can C++ have code in the global scope?

查看:187
本文介绍了C ++可以在全局范围内使用代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有代码(在C ++源文件的全局范围内编译为汇编指令是否合法)?以前,我的印象是,除了为什么我不能分配C中某个函数外部的全局变量的值?

原始问题的回答者断言,您不能拥有超出函数范围的代码.我认为我对此并不完全了解规则,以及什么才算是代码".

int foo() {
    cout << "Inside foo()" << endl;
    return 5;
}

// is this not code?
int global_variable = foo();

// How does this statement work without generating code?
int a = 4;
int b = 5;
int c = a + b;

int main() {
    // The program behaves as if the statements above were executed from
    // top to bottom before entering the main() function.
    cout << "Inside main()" << endl;
    cout << "int c = " << c << endl;
    return 0;
}

解决方案

您链接到的问题的答案是以一种简单的方式进行的,而不是对结构使用严格的C ++命名.

C ++没有更多的学问依据,它没有代码". C ++具有声明,定义和语句.语句可能是您认为的代码":iffor,表达式等.

仅声明和定义可以出现在全局范围内.当然,定义可以 include 表达式. int a = 5;定义一个由表达式初始化的全局变量.

但是您不能像a = 5;那样在全局范围内拥有随机的语句/表达式.也就是说,表达式可以是定义的一部分,但表达式不是定义.

您当然可以在main之前调用函数.太复杂而无法在编译时执行的全局变量构造函数和初始化器必须在main之前运行.例如:

int b = []()
{
    std::cout << "Enter a number.\n";
    int temp;
    std::cin >> temp;
    return temp;
}();

编译器在编译时无法执行此操作;它是交互式的. C ++要求在main开始之前初始化所有全局变量.因此,编译器将不得不调用main之前的代码.这是完全合法的.

每个C ++编译和执行系统都有某种机制,可以在之前调用代码,在main之后调用代码.全局变量必须被初始化,并且可能需要调用对象构造函数来进行该初始化. main完成后,必须销毁全局变量,这意味着需要调用析构函数.

Is it legal to have code (which compiles to assembly instructions in the global scope of a C++ source file? Previously, I was under the impression that except for the Ch programming language (an interpreter for C/C++), you cannot have code in the global scope of a C++ program. Code/instructions can only be inside the body of a function [period]!

However, I found out that you can call functions before the main function in C++ by assigning them to a global variable! This would involve a call instruction in the assembly code. Also you can assign the sum of two variables into another global variable outside the assembly code. That would almost certainly involve an add and mov instructions. And if that code is in the global scope, outside of any function, when would it execute? If the + were an overloaded operator of a class type, if it had a print statement inside of it, when would that execute?

Also can you have loops and control structures in the global scope of a C++ program, and if so when are they executed? What about for other program constructs, are they allowed in the global scope, and under what circumstances, and when are they executed?

This question is in a response to a previous question that I posted: Why can't I assign values to global variables outside a function in C?

The answerer to the original question asserts that you cannot have code outside of the scope of a function. I think that I do not fully understand the rules for this, and what exactly is considered to be "code" or not.

int foo() {
    cout << "Inside foo()" << endl;
    return 5;
}

// is this not code?
int global_variable = foo();

// How does this statement work without generating code?
int a = 4;
int b = 5;
int c = a + b;

int main() {
    // The program behaves as if the statements above were executed from
    // top to bottom before entering the main() function.
    cout << "Inside main()" << endl;
    cout << "int c = " << c << endl;
    return 0;
}

解决方案

The answer on the question you linked to was talking in a simple way, not using strict C++ naming for constructs.

Being more pedantic, C++ does not have "code". C++ has declarations, definitions, and statements. Statements are what you probably think of as "code": if, for, expressions, etc.

Only declarations and definitions can appear at global scope. Of course, definitions can include expressions. int a = 5; defines a global variable, initialized by an expression.

But you can't just have a random statement/expression at global scope, like a = 5;. That is, expressions can be part of definitions, but an expression is not a definition.

You can call functions before main of course. Global variable constructors and initializers which are too complex to be executed at compile time have to run before main. For example:

int b = []()
{
    std::cout << "Enter a number.\n";
    int temp;
    std::cin >> temp;
    return temp;
}();

The compiler can't do that at compile-time; it's interactive. And C++ requires that all global variables are initialized before main begins. So the compiler will have to invoke code pre-main. Which is perfectly legal.

Every C++ compilation and execution system has some mechanism for invoking code before and after main. Globals have to be initialized, and object constructors may need to be called to do that initialization. After main completes, global variables have to be destroyed, which means destructors need to be called.

这篇关于C ++可以在全局范围内使用代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆