Define a core function in a class

I was wondering if it is possible to define inside main()something like:

struct runtime_entry_point
{
    friend int main()
    {

    }
};

I tested this and it does not work (almost in GCC 4.8.2):

g ++ -o dist / Release / GNU-Linux-x86 / turbo build / Release / GNU-Linux-x86 / main.o / usr / lib / gcc / x 86_64-unknown-linux-gnu / 4.8.2 /../ ../../../lib/crt1.o: In the function `_start ': collect2: error: ld exited with status 1

It sounds to me like defining a mistake main().

Later I wrote the main classic version:

struct runtime_entry_point
{
    friend int main()
    {

    }
};

int main(){}

Now compilation failed because it is int main()already defined inside a struct runtime_entry_point! What is going on here?

+4
source share
3 answers

cppreference.com :

struct runtime_entry_point
{
    friend int main()
    {
    }
};

, , . , , .

main() ( ), main() ( ).

+2

main /. main , . , main /. , .

, , " " . (main()) .

+4

, , /:

#include <iostream>

struct runtime_entry_point
{
    friend int main();
};

int main()
{
    std::cout << "Hello, world!" << std::endl;
}

GCC.

0

Source: https://habr.com/ru/post/1539874/


All Articles