如何使大量现有函数在类的范围内可用?
我需要将一个大型(数百个源文件)项目放入一个库中,通过将数十个全局变量全部放入一个类对象中来删除它们。
问题是现在有大约一千个函数需要成为这个类的成员,以便它们能够访问对象变量。
除了将 MyClass::
添加到源文件中的每个函数定义之外,是否有一种方法可以欺骗并指示特定源文件中的所有函数都应该是 MyClass< 的一部分/代码> 范围?
I need to make a large (100's of source files) project into a library, removing dozens of global variables by putting them all into a class object.
The problem is the thousand or so functions that now need to be members of this class
so they have access to the object variables.
Otehr than adding MyClass::
to every single function definition in the source files, is there a way to cheat and indicate that all the functions in a particular source file should be part of the MyClass
scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将所有全局变量添加到
命名空间
。无论您想访问什么文件,请
在头文件中执行以下操作:通过这种(
使用命名空间
)方式,您可以指示所有变量都应该可以访问,而无需对该文件使用作用域解析::
。 (即您可以简单地访问该文件内的g_i
而不是MyGlobals::g_i
)。另请注意,您必须在
.cpp
文件中定义所有全局变量:Add all the globals to a
namespace
.Whatever files you want to access, do:
inside the header file. In this (
using namespace
) way you can indicate that all the variables should be accessible without using scope resolution::
for that file. (i.e. you can simply accessg_i
instead ofMyGlobals::g_i
inside that file).Also note that, you have to define all the global variables inside a
.cpp
file: