Two main functions

前端 未结 8 833
孤独总比滥情好
孤独总比滥情好 2020-12-19 10:20

Can we have two main() functions in a C++ program?

相关标签:
8条回答
  • 2020-12-19 10:50

    Yes! Why not?

    Consider the following code:

     namespace ps
     {
         int main(){return 0;}
     }
    
     int main()
     {
         ps::main();
     }
    

    Its ::main() that will be called during execution.

    0 讨论(0)
  • 2020-12-19 10:51

    The standard explicitly says in 3.6.1:

    A program shall contain a global function called main, which is the designated start of the program. [...] This function shall not be overloaded.

    So there can one only be one one main function in the global scope in a program. Functions in other scopes that are also called main are not affected by this, there can be any number of them.

    0 讨论(0)
提交回复
热议问题