开发者

Can we overload main() function in C++? [duplicate]

开发者 https://www.devze.com 2023-02-16 04:40 出处:网络
This question already has answers here: Is main() overloaded in C++? (6 answers) 开发者_JS百科 Closed 6 years ago.
This question already has answers here: Is main() overloaded in C++? (6 answers) 开发者_JS百科 Closed 6 years ago.

Since C+++ allows function overloading, can we overload main()?

For example,

int main(const std::string &)
{
   return 0;
}
int main(int argc, char *argv[])
{
  return main("calling overloaded main");
}

gcc-4.3.4 doesn't compile this, and gives these errors: (see at ideone)

prog.cpp:4: error: first argument of ‘int main(const std::string&)’ should be ‘int’

prog.cpp:4: error: ‘int main(const std::string&)’ takes only zero or two arguments

prog.cpp: In function ‘int main(int, char**)’:

prog.cpp:8: error: declaration of C function ‘int main(int, char**)’ conflicts with

prog.cpp:4: error: previous declaration ‘int main(const std::string&)’ here

prog.cpp: In function ‘int main(int, char**)’:

prog.cpp:10: error: invalid conversion from ‘const char*’ to ‘int’

prog.cpp:8: error: too few arguments to function ‘int main(int, char**)’

prog.cpp:10: error: at this point in file

So I'm wondering if the C++ Standard explicitly forbids overloading of main? If so, which statement?


Yes it explicitly forbids that. Refer to 3.6.1p2

An implementation shall not predefine the main function. This function shall not be overloaded.


This way, the main function's name can stay unmangled. That is, the runtime library can call a symbol having a fixed name (e.g main or _main) to jump to the main function. The libraries' code will not need to depend on what parameter list the program's main function has.

The implementation is also allowed to define additional valid parameter lists for the main function (The POSIX spec specifies a char **env parameter for the environment variables, for example). It would not be clear when an overload of main is a "non-main function" or whether it's a "main function", thus an entry point. Presumably, you would want to get an error if you would declare more than one entry point, so such issues are important.


What you've done is declare two entry points to the program's execution. This is forbidden by the compiler since when you run the program the program will not know where to begin!

I also can't see any reason why you would want to do this in your application's design.


As per me global main function(main function outside all classes) cannot in overloaded in c++, But if you write main function inside a class then it will compile fine however it will not be treated as program entry point for example the following code will not compile file name mainoverloaderror.cpp

#include<iostream>
using namespace std;
int main(int noofarg,char *values[])
{
    std::cout<<"hello "<<endl<<values[0]<<endl<<values[1]<<endl<<noofarg;
    return 0;
}
int main()
{
 std::cout<<"hello main()";
    return 0;
}

compilation error : mainoverloaderror.cpp: In function ‘int main()’: mainoverloaderror.cpp:13: error: declaration of C function ‘int main()’ conflicts with mainoverloaderror.cpp:7: error: previous declaration ‘int main(int, char**)’ here

Look at this code main function inside a class. Although it will not have multiple entry point but will compile fine:

#include<iostream>

using namespace std;

class MainClass{

        int main1()
    {

        std::cout<<"hello main()"<<endl;
        return 0;
    }
    int main(int noofarg,char *values[])
    {

        std::cout<<"hello "<<endl<<values[0]<<endl<<values[1]<<endl<<noofarg;
        return 0;
    }
    int main()
    {

     std::cout<<"hello main()";
        return 0;

    }
};

int main()
{

 std::cout<<"hello main()";
    return 0;

}

so to conclude: in c++ global main cannot be overloaded it will generate compile time error, in is because you cannot have multiple entry point for the same program as said above.

0

精彩评论

暂无评论...
验证码 换一张
取 消