Saturday, November 27, 2010

Almighty Constructor...

A quick look at the starting point of a flight-simulator (written in C++) showed the following - and I saw this simulator running before analyzing the code, so I knew that this main(...) must do something...:


int main(int argc, char* argv[])
{
FSim sim;
}


Huh?

For those of you with too little imagination - here's the explanation:
The constructor of the class FSim started and ran the whole simulator by constructing other objects, which themselves did the same. So the whole program was an incredibly long chain of constructors and quitting the program just let the innermost constructor run to completion, which in turn let all the others run to completion, which resulted in a constructed variable sim, which ended the program.

Only the best of all developers are able to write such extremely clever solutions!

No comments:

Post a Comment