0

is there any way to hide the WinMain() function inside a class? Thank you.

Anon
  • 1,274
  • 4
  • 17
  • 44
  • You should elaborate a bit more of what you want to achieve and why and maybe show some example code. – ChrisWue Dec 27 '11 at 00:22
  • From what should it be hidden? – zneak Dec 27 '11 at 00:22
  • Yes, that's possible. Why you'd want to rewrite the CRT code to accomplish this is deeply mysterious. – Hans Passant Dec 27 '11 at 00:23
  • 2
    Why all the downvotes? You might disagree with what he's trying to do, but the question seems fine to me. I understand what he's asking, and it's a question that can actually be answered. Why does that deserve downvotes? – jalf Dec 27 '11 at 00:26
  • 2
    Why are you trying to "hide" your entry point? What's wrong with just writing a one-line `WinMain` that calls a class member function where all the work is done? BTW, `winmain()` is not the same function as `WinMain`, in C++ capitalization counts. – Ben Voigt Dec 27 '11 at 00:27
  • im trying to write a game engine. in the future i want to add multi-platform flexibility to it - thats the reason behind hiding the `winmain()` function – Anon Dec 27 '11 at 01:00
  • Since you'll have much more platform specific code than just WinMain you'll want to develop a strategy to deal with that. A common way is to keep it separate from platform agnostic code and only compile/link those files when building for that platform. – Retired Ninja Dec 27 '11 at 02:06
  • u mean using something like #ifdef and #endif? – Anon Dec 27 '11 at 02:14
  • 1
    Although you can use #ifdef/#endif to split platform specific code out it tends to not look very clean if you do it a lot. Having a header that defines an interface and different cpp files for each platform generally works better. – Retired Ninja Dec 27 '11 at 03:10
  • @RetiredNinja: That works ok for an OS abstraction layer, but not for startup code. If you try to use the "interface/implementing class" technique for the startup code, you end up with questions like this one. – Ben Voigt Dec 27 '11 at 04:15

1 Answers1

2

Nope, there isn't. A better question is "why would you want to"? In Java or C#, the main function is a static class member, not because it is better or cleaner, but because the language does not allow non-member functions. In C++, they are allowed, and even encouraged in many cases.

It makes more sense for the main function, the entry point to your program, to be "outside". It starts in a basically empty environment, and then it sets up anything that needs to be set up before calling into the actual application logic.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • 1
    Windows finds the entry-point using its address stored in the PE header, not by name. So it can actually be any function with the right calling convention (although since it never returns, calling convention isn't terribly important either). One can easily imagine passing the name of a class static member function to the linker to be used as the entry point. – Ben Voigt Dec 27 '11 at 00:29
  • @BenVoigt: true, but then it's no longer `WinMain`. ;) plus, anything that can't be done by fiddling with the source code is a bit of a hack. But yeah, you're right, it *can* be done – jalf Dec 27 '11 at 00:34