Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Win32
Hi guys, I had this bothersome question.
 
#include <Windows.h>
namespace nSpace
{
	// Creates and registers the window
	class Init
	{
	private:
		BYTE CreateWindow();
	};
}
 
The problem with this code, is that I can't create a function called "CreateWindow" because in the global scope (that is, in one of the Windows' header files) there is a samely named function.
So can't I make it work it out a different way? Is there any solution so as to keep that name which is very convenient to me ??
Aren't supposed that namespaces and classes can encapsulate names of functions in their scope without interfering with any other and not having to resort to the parent's scope ones?
See you around.
Posted 10-Feb-13 9:39am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The problem is that CreateWindow has already been declared as a C Preprocessor macro in WinUser.h
...
#ifdef UNICODE
#define CreateWindow CreateWindowW
#else
#define CreateWindow CreateWindowA
#endif
...
and C Preprocessor macros being an old school C feature take no account of namespaces.
 
There are 2 ways to deal with this. Either use a different name, I use mxCreateWindow for historical reasons or get rid of the macro definition before the parser finds your function.
#undef CreateWindow
That might seem simple enough but if you also use the global namespace CreateWindowW or CreateWindowA functions by using the name CreateWindow that code could now be broken because you've take away the macro that defined part of it.
One way round this is to use
#pragma push_macro("CreateWindow")
before your code and
#pragma pop_macro("CreateWindow")
after your code but this is very Microsoft Visual C++ specific and may not even work reliably with all versions of that so consider carefully.
  Permalink  
Comments
unscathed18 at 10-Feb-13 15:49pm
   
I think I'll stick with mxCreateWindow.
Although, The #pragma push_macro and pop_macro doesn't work for me (I use Visual C++ Express 2010) Do you know why it is it?
Matthew Faithfull at 10-Feb-13 16:57pm
   
Don't know why, I've had issues with them when I tried to make them work as well, probably VS2008 express back then. There's some trick to it I guess but I get suspicious when I see Microsoft's own code going around the houses where they could have used the pragmas and don't. Then again in other places they do use them so who knows. Probably best to stick with renaming to avoid the clash.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 424
1 ridoy 410
2 DamithSL 240
3 OriginalGriff 179
4 Suvendu Shekhar Giri 150
0 OriginalGriff 8,033
1 DamithSL 6,179
2 Sergey Alexandrovich Kryukov 5,538
3 Maciej Los 5,508
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100