功能和名称空间冲突 [英] Function and namespace conflict

查看:60
本文介绍了功能和名称空间冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有这个麻烦的问题。



Hi guys, I had this bothersome question.

#include <Windows.h>
namespace nSpace
{
	// Creates and registers the window
	class Init
	{
	private:
		BYTE CreateWindow();
	};
}





这段代码的问题在于我无法创建一个名为CreateWindow的函数,因为在全局范围(也就是说,在一个Windows的'头文件中)有一个同名的函数。

所以我不能让它以不同的方式工作吗?是否有任何解决方案以保持这个名称对我来说非常方便?

我们不应该认为名称空间和类可以在其范围内封装函数名称而不会干扰任何其他名称不得不诉诸父母的范围?

见到你。



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.

推荐答案

问题在于 CreateWindow 已在WinUser.h中声明为C预处理器宏

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
...



和C预处理器宏作为旧学校C功能不考虑命名空间 s。



有两种方法可以解决这个问题。使用不同的名称,我使用 mxCreateWindow 出于历史原因或在解析器找到您的函数之前摆脱宏定义。


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



这看起来很简单但是如果你还使用名称CreateWindow来使用全局命名空间CreateWindowW或CreateWindowA函数,那么现在代码可以被破坏,因为你已经拿走了定义其中一部分的宏。

一种方法是使用


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")

<你的代码之后是
,但这是特定于Microsoft Visual C ++的,甚至可能无法在所有版本中可靠地工作,所以请仔细考虑。


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.


这篇关于功能和名称空间冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆