[英]Int main inside a class
Simple question, how can I run a program having main inside a class? 简单的问题,我怎样才能在一个类中运行一个主程序? I have a code:
我有一个代码:
MojSwiat2.cpp: MojSwiat2.cpp:
int Main::main() {
// code
return 0;
}
And MojSwiat2.h: 和MojSwiat2.h:
class Main {
public:
int main();
};
Main run;
int Main::main() { // with this I have error: function int Main::main(void) already has a body
run.main();
} // and without I got unresolved external symbol _main referenced in function __tmainCRTStartup
Reason I need to do this: Accessing protected members of class from main 我需要这样做的原因: 从main访问类的受保护成员
By defining a normal main
that only contains a call to your other function. 通过定义一个只包含对其他函数的调用的普通
main
。 Like this: 像这样:
int main(int, char**) {
return Main().main();
}
int main(int argc, char* argv[])
{
Main m;
return m.main();
}
or if Main::main is declared static 或者如果Main :: main声明为static
int main(int argc, char* argv[])
{
return Main::main();
}
You still need to define main
. 你仍然需要定义
main
。
class my_app {
int main(int argc, char* argv[])
{
// ...
}
}
my_app app;
int main(int argc, char *argv[])
{
return app.main(argc, argv);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.