我可以将 main() 中的代码替换为全局对象的构造函数吗?
假设 init
是在 main()
之前初始化的最后一个全局对象(并且我们不需要任何命令行参数),我可以吗做类似的事情:
struct int_main {
int_main ()
{
//... start code execution
}
}init;
int main ()
{
}
以这种方式提出问题,因为我有兴趣知道 main()
是否保证除以下内容之外的任何内容:
- 来自命令行的 argc、argv
- 所有全局对象都已初始化 在它之前
Assuming that init
is the last global object being initialized before main()
(and we don't need any command line argument), can I do something like:
struct int_main {
int_main ()
{
//... start code execution
}
}init;
int main ()
{
}
Asking the question in this way, because I am interested in knowing if main()
assures anything other than:
- argc, argv from command line
- All global objects are initialized
before it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法保证所有全局对象都是在您的对象之前构造的,因此您不能使用它们中的任何一个。这包括 iostream 的重要部分。
我通常使用的模式是
main()
只是构造应用程序对象并调用它的方法来执行实际工作。You do not have a guarantee that all global objects are constructed before your object, so you may not use any of them. That includes vital parts of iostreams.
I normally use the pattern that
main()
just constructs the application object and invokes a method on it to perform the actual work.您将很难从int_main
构造函数中捕获任何异常。此外,您也将很难返回进程退出代码并完全展开堆栈。
这主要是
main
在 C++ 中提供的功能:捕获异常的地方,以及使用指定的进程退出代码正常返回(而不仅仅是exit
)的方法。干杯&呵呵,
You would have a hard time catching any exception from theint_main
constructor.Also you would have a hard time returning a process exit code with complete unwinding of the stack.
That's mainly what
main
provides in C++: a place to catch exceptions, and a means to return normally (not justexit
) with a specified process exit code.Cheers & hth.,
在 C\C++ 中,您可以通过 Visual Studio IDE 声明应用程序的入口点。按照惯例,代码的入口点要么是 Main,要么是 win32 exe WinMain。
为了回答你的问题,CRT 将按照
C 基元类型的顺序初始化所有全局变量
C 结构类型和/或 C++ 类类型
调用类构造函数
调用应用程序的入口点,这将通过 CRTStartup 完成(如果我错了,请纠正我)
Within C\C++ you can declare the entry point of your application via the visual studio IDE. It's the convention that the entry point into you're code will be either be Main or in the case of a win32 exe WinMain.
As to answer your question, the CRT will iniailize all global variables in the order of
C Primitive Types
C Struct Types and or C++ Class types
Call the class constructors
Call the Entry point into your application, this will be done from CRTStartup (correct me if i'm wrong)
理论上这是可能的,但是您不能确定全局对象的初始化顺序,因此您无法保证最后初始化哪个对象,因此您在运行全局对象内的“main”在调用其构造函数之前可能没有正确的程序设置状态。此外,如果发生这种情况,您将无法暂停“主”对象构造函数的执行以等待正确的起始状态存在。
此外,由于操作系统的运行时将调用您实际的
main()
函数,以便实际“运行”您的程序,无论是否存在“主”全局对象,您都将需要从“main()”返回一个值,以便操作系统运行时确定程序的退出状态。In theory this is possible, but you're not assured of the initialization order of global objects, so you are not going to have an assurance of which object will be initialized last, and therefore you're going to have a problem with running a "main" inside a global object that may not have the proper state of the program setup before its constructor is called. Furthermore, you're not going to be capable of suspending the execution of your "main" object's constructor to wait for the proper starting state to exist if such a scenario occurs.
Additionally, since the OS's runtime will be calling your actual
main()
function in order to actually "run" your program regardless of the presence of a "main" global object or not, you're going to need to return a value from 'main()` in order for the OS runtime to determine the exit status of your program.