You can create a class with a member function named main
, but it will not be the main
that gets invoked to start your program -- that needs to be a global function (outside any class or namespace). But yes, a member function named main
is perfectly fine (ยง3.6.1/3):
The name main
is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main
, as can entities in other namespaces. -- end example]
As far as how to arrange your code, you typically end up with something like this:
class C1 {
int main();
};
class C2 : public C1 {
public:
int foo();
// or perhaps: static int foo();
};
int C1::main() {
C2::foo(); // given `C2::static int foo();`
// otherwise: C2 c; c.foo();
}