Call a function before main [duplicate]

You can have a global variable or a static class member.

1) static class member

//BeforeMain.h
class BeforeMain
{
    static bool foo;
};

//BeforeMain.cpp
#include "BeforeMain.h"
bool BeforeMain::foo = foo();

2) global variable

bool b = foo();
int main()
{
}

Note this link – Mirror of http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14 / proposed alternative – posted by Lundin.

Leave a Comment