Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

Initialize pointers to NULL in Managers.cpp

(1/5) > >>

280Z28:
In sdk/Managers/Managers.cpp, it is good practice to initialize the global pointers to NULL.


--- Code: ---Manager* g_Manager = NULL;
wxConfigBase* g_Config = NULL;
TemplateManager* g_TemplateManager = NULL;
PluginManager* g_PluginManager = NULL;
EditorManager* g_EditorManager = NULL;
MacrosManager* g_MacrosManager = NULL;
MessageManager* g_MessageManager = NULL;
ProjectManager* g_ProjectManager = NULL;
ToolsManager* g_ToolsManager = NULL;

--- End code ---

thomas:
Actually, you would use 0 rather than NULL which is defined as ((void*)0).

All Managers accessible via the Managers class are initialised via Set , but you should use Manager::Get()->GetXXX() anyway.

Urxae:

--- Quote from: thomas on December 13, 2005, 12:16:35 am ---Actually, you would use 0 rather than NULL which is defined as ((void*)0).

--- End quote ---

Actually, what exactly NULL is defined as is implementation-dependant, but on C++ compilers it's usually (if not always) 0, as ((void*)0) doesn't implicitly convert to every other pointer type anymore :P.

[one quick Google later]
Bjarne says NULL defined as 0 in C++, but he uses plain 0 anyway basically because macros are evil1. I'm with him ;).

1) Yeah, so he didn't say it quite that strongly. Sue me.

thomas:
If you had looked in windef.h, you would know better...

NULL is defined as ((void*) 0) which is not valid. This is one reason why I said so (and of course because I think like Stroustrup regarding macros). In fact, this is a good example of how macros can be evil, because it will break your compile in certain situations, and it is not obvious why.

Another reason is that you explicitely cast from void* to Type* when it is not necessary. Of course assigning 0 is not really any different internally, but casting from void* to anything else unless you really have to is ideologically evil. The last reason is that assigning 0 is universal (works with every type) while assigning ((void*) 0) is not.

takeshimiya:
I do not like to use macros, but I wonder what can I use to replace them in:

-Preventing multiple header inclusion. (Why compilers doesn't prevent it?)
-Compile time options (like #ifdef UNICODE ...)
-Should I use things like
#define DEFAULT_BATCH_BUILD_ARGS _T("-na -nd -ns --batch-build-notify")
or
const wxString DEFAULT_BATCH_BUILD_ARGS(_T("-na -nd -ns --batch-build-notify"));
?
Should I use the first, because the latter would pollute the global namespace whenever the #define won't?

Navigation

[0] Message Index

[#] Next page

Go to full version