Skip to content

Instantly share code, notes, and snippets.

@jpmec
Created July 26, 2013 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmec/6090738 to your computer and use it in GitHub Desktop.
Save jpmec/6090738 to your computer and use it in GitHub Desktop.
A simple example demonstrating how to print "Hello World" before the main() function is called.
/** Example showing how to print "Hello World" before main().
* Expected output:
* Hello World
* Hello from main()
*
* Compile with:
* g++ -Wall test_cpp_print_before_main.cpp -o test_cpp_print_before_main
*/
#include <iostream>
using namespace std;
/// A simple class that prints "Hello World" when constructed.
class HelloWorld
{
public:
HelloWorld(void)
{
cout << "Hello World" << endl;
}
};
// Construct a const instance,
// will call constructor before main() is entered.
const HelloWorld hello_world = HelloWorld();
int main(int argc, const char* argv[])
{
cout << "Hello from main()" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment