使用指令类静态函数?
我使用的 API 在名为 TCODConsole
的类中具有很多函数作为静态函数。现在我认为它在命名空间中,所以我写道:using namespace TCODConsole;
。然后我发现 TCODConsole
不是一个命名空间,而是一个类。
有没有一种方法可以像使用 using namespace
一样导入这些函数?
I am using an API that has a lot of functions in a class named TCODConsole
as static functions. Now I thought that it was in a namespace, so I wrote: using namespace TCODConsole;
. Then I found out that TCODConsole
ain't a namespace, but a class.
Is there a way to import those functions in a similar way as you would use using namespace
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,没有捷径可以调用
myStaticFun()
来代替MyClass::myStaticFun()
。你不能在课堂上做到这一点。它是一个类,而不是一个命名空间。但你可以写一些类似包装的东西。也就是说,您将添加具有相同名称的函数并从该函数调用静态方法。像这样的事情:这不是一个很好的方法。就我个人而言,我认为最好坚持上课而不是这样做。
No, there is no shortcut way to call
myStaticFun()
instead ofMyClass::myStaticFun()
. You cannot do that with class. It's a class, not a namespace. But you can write something like a wrapper. That is you will add functions with same name and call the static methods from that functions. Something like this:Not a very good way. Personally I think it is better to stick with the class instead of doing this.
虽然我可能误解了这个问题,
如果缩短资格是目标,
像下面这样的
typedef
能达到目的吗?或者,如果可以实例化类
TCODConsole
,因为
static
成员函数可以用与以下相同的形式调用非静态成员函数,下面的代码可能可以满足目的:
Though I may misunderstand the question,
if shortening the qualification is the objective,
does
typedef
ing like the following meet the purpose?Alternatively, if the class
TCODConsole
can be instantiated,since
static
member function can be called with the same form asnon-static member function, the following code might meet the purpose:
我想不出一个干净的方法来做到这一点,但我可以想到一个有点丑陋的黑客,只要使用它的代码是类的一部分(并且如果 TCODConsole 确实只包含 静态成员函数)。假设您是
bar()
,它是Foo
类的成员函数,并且您想要调用一个函数baz()
,它是TCODConsole 的静态成员,但没有完全限定它。您可以做的是从 TCODConsole 私下派生 Foo:是的,那很丑。 :(
如果你想使用 Boost.PP(Boost 预处理器宏库 - 你不需要编译或包含 Boost 中的任何其他内容来使用它),你可能可以制作一个不那么丑陋但更复杂的宏,将为您“导入”这些函数,方法是将它们用内联函数包装在另一个命名空间中(然后您可以随意导入),它仍然需要您显式指定要“导入”的每个函数的名称,因此。您的代码(减去宏)将如下所示:
然后您就可以在任何您想要的地方编写
using namespace TCODStatic;
。I can't think of a clean way to do it, but I can think of a kinda ugly hack that will work, as long as the code using it is part of a class (and if TCODConsole really contains only static member functions). Let's say you are
bar()
, a member function of the classFoo
, and you want to call a functionbaz()
which is a static member of TCODConsole without fully-qualifying it. What you can do is to privately derive Foo from TCODConsole:Yeah, that's ugly. :(
If you want to use Boost.PP (the Boost Preprocessor macro library - you don't need to compile or include anything else from Boost to use it), you can probably make a less ugly but quite a bit more convoluted macro which will "import" these functions for you, by wrapping them with inline functions inside another namespace (which you can then import at will). It would still require you to explicitly specify the name each of the functions you want to "import", so your code (minus the macro) will look something like this:
and then you'll be able to write
using namespace TCODStatic;
anywhere you want.简短的回答:不使用宏就不行。 C++ 语言中没有适当的机制可以做这样的事情:
您可以使用宏来为您构建这些表达式:
但是我不鼓励这样做,因为如果您广泛使用此类宏,它可能会成为维护噩梦。您最终会得到如下代码:
...其中这些内容都没有在程序中的任何位置定义为一流名称。您已经失去了所有类型检查,代码使用了只有您知道的“秘密语言”,并且变得难以调试、扩展和修复。
编辑:
你可以写一种桥。像这样:
Short answer: not without using macros. There is no mechanism in the C++ language proper to do something like this:
You could use a macro to build these expressions for you:
I would discourage this however, because it can become a maintennence nightmare is you make extensive use of such macros. You end up with code like this:
...where none of these things are defined as first-class names anywhere in the program. You've lost all type checking, the code uses a "secret language" that only you know, and it becomes difficult to debug, extend and fix.
EDIT:
You could write a kind of bridge. Like this: