你可以在C ++中打印任何东西,然后才能进入主函数? [英] Can you print anything in C++, before entering into the main function?
本文介绍了你可以在C ++中打印任何东西,然后才能进入主函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在进入主要功能之前,您可以在C ++中打印任何内容吗?
Can you print anything in C++, before entering into the main function?
这是彭博的采访问题:
答案
:创建一个全局变量,从printf语句中赋予一些内容。
Answer :create a global variable assigning value from printf statement with some content.
推荐答案
#include <iostream>
struct X
{
X()
{
std::cout << "Hello before ";
}
} x;
int main()
{
std::cout << "main()";
}
这个格式良好的C ++程序打印
This well-formed C++ program prints
你好,在main()之前
Hello before main()
你看,C ++标准保证命名空间范围变量的构造函数(在这个例子中,它的 x
)将在 main()
之前执行。因此,如果你在这样的对象的构造函数中打印东西,它将在 main()
之前打印。 QED
You see, the C++ standard guarantees that the constructors of namespace-scope variables (in this example, it's x
) will be executed before main()
. Therefore, if you print something in a constructor of such an object, it will be printed before main()
. QED
这篇关于你可以在C ++中打印任何东西,然后才能进入主函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文