Re:Who Needs Main() Anyway?
by xjwm on Saturday January 21, 2023 @10:05AM
(#34466871)
I've written code like this in C to create library functions that will be linked in to other projects, but where there is no 'main' function in the C file. Do people use C++ the same way, or is it best practice to include 'main?'
[ Parent
| on HN ]
Re:Who Needs Main() Anyway?
by MathMonkeyMan on Saturday January 21, 2023 @12:58PM
(#34468448)
It's the same as in C. The observation here is that variables with static storage duration can be initialized by running user code before main() begins.
Oh! Thank you. That makes much more sense now.
Re:Who Needs Main() Anyway?
by colejohnson66 on Sunday January 22, 2023 @01:48PM
(#34480807)
It’s also the same in .NET. If your Main() function references types that have a static constructor, the constructors could run before Main().
It’s a surprising thing when one first discovers it, but it makes a lot of sense.