gpt4 book ai didi

c++ - 在 main 之前调用一个函数

转载 作者:IT老高 更新时间:2023-10-28 12:30:25 26 4
gpt4 key购买 nike

Possible Duplicate:
Is main() really start of a C++ program?

可以在程序启动之前调用我的函数吗?我如何在 C++C 中完成这项工作?

最佳答案

你可以有一个全局变量或一个static类成员。

1) 静态类成员

//BeforeMain.h
class BeforeMain
{
static bool foo;
};

//BeforeMain.cpp
#include "BeforeMain.h"
bool BeforeMain::foo = foo();

2) 全局变量

bool b = foo();
int main()
{
}

注意这个链接 - Mirror of http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14/proposed alternative - 由 Lundin 发布。

关于c++ - 在 main 之前调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10897552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com