Is it ok to use this as a global variable?

 
 HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
Last edited on
It's hard to tell without the context of the application you use it in. It should be valid code, I can't tell whether it's the cleanest solution though, for that we'll need a context.
> Is it ok to use this as a global variable?
> HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

Yes.

If it is used in only one translation unit, give it internal linkage.
namespace { HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); }


Otherwise, place it at namespace scope in a header:
1
2
3
4
5
6
7
8
namespace utility
{
    // one per translation unit
    namespace { HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); }
    
    // or declare it extern, and initialise in the .cpp file
    // extern HANDLE console ;
}; 
Excellent, thank you.
Topic archived. No new replies allowed.