In C++ why don’t we ever place the main method inside a class (like Java)? Why doesn’t doing so make sense (I think)?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
We can.
mainis not a reserved word. But by the language standard, the C++ toolchain expects the entry point of the program to bemainin the global scope. So themaininside a class won’t be recognized as the program’s entry point.Feel free to define a class method called
main, and call it from the globalmain.This design comes all the way from C. Compatibility with existing C code was a major design goal of C++ early on, and there was hardly any real benefit to changing the entry point convention. So they kept the C standard in place. And like everyone said, C++, unlike Java, does perfectly allow for standalone (i. e. non-class) functions.