使用指令类静态函数?

发布于 2024-11-09 01:55:20 字数 245 浏览 0 评论 0原文

我使用的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

池木 2024-11-16 01:55:20

不,没有捷径可以调用 myStaticFun() 来代替 MyClass::myStaticFun()。你不能在课堂上做到这一点。它是一个类,而不是一个命名空间。但你可以写一些类似包装的东西。也就是说,您将添加具有相同名称的函数并从该函数调用静态方法。像这样的事情:

class MyClass {
    static void fun();
};

void fun() {
    MyClass::fun();
}

// call it
fun();

这不是一个很好的方法。就我个人而言,我认为最好坚持上课而不是这样做。

No, there is no shortcut way to call myStaticFun() instead of MyClass::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:

class MyClass {
    static void fun();
};

void fun() {
    MyClass::fun();
}

// call it
fun();

Not a very good way. Personally I think it is better to stick with the class instead of doing this.

与君绝 2024-11-16 01:55:20

虽然我可能误解了这个问题,
如果缩短资格是目标,
像下面这样的typedef能达到目的吗?

struct TCODConsole {
  static void f();
  static void g();
};

int main() {
  typedef TCODConsole T;
  T::f();
  T::g();
}

或者,如果可以实例化类 TCODConsole
因为 static 成员函数可以用与以下相同的形式调用
非静态成员函数,下面的代码可能可以满足目的:

int main() {
  TCODConsole t;
  t.f();
  t.g();
}

Though I may misunderstand the question,
if shortening the qualification is the objective,
does typedefing like the following meet the purpose?

struct TCODConsole {
  static void f();
  static void g();
};

int main() {
  typedef TCODConsole T;
  T::f();
  T::g();
}

Alternatively, if the class TCODConsole can be instantiated,
since static member function can be called with the same form as
non-static member function, the following code might meet the purpose:

int main() {
  TCODConsole t;
  t.f();
  t.g();
}
风情万种。 2024-11-16 01:55:20

我想不出一个干净的方法来做到这一点,但我可以想到一个有点丑陋的黑客,只要使用它的代码是类的一部分(并且如果 TCODConsole 确实只包含 静态成员函数)。假设您是 bar(),它是 Foo 类的成员函数,并且您想要调用一个函数 baz(),它是TCODConsole 的静态成员,但没有完全限定它。您可以做的是从 TCODConsole 私下派生 Foo:

class Foo : private TCODConsole
{
    void bar()
    {
        baz();
    }

};

是的,那很丑。 :(

如果你想使用 Boost.PP(Boost 预处理器宏库 - 你不需要编译或包含 Boost 中的任何其他内容来使用它),你可能可以制作一个不那么丑陋但更复杂的宏,将为您“导入”这些函数,方法是将它们用内联函数包装在另一个命名空间中(然后您可以随意导入),它仍然需要您显式指定要“导入”的每个函数的名称,因此。您的代码(减去宏)将如下所示:

namespace TCODStatic
{
    IMPORT_FROM_TCOD(foo)
    IMPORT_FROM_TCOD(bar)
    IMPORT_FROM_TCOD(baz)
    IMPORT_FROM_TCOD(spaz)
}

然后您就可以在任何您想要的地方编写 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 class Foo, and you want to call a function baz() which is a static member of TCODConsole without fully-qualifying it. What you can do is to privately derive Foo from TCODConsole:

class Foo : private TCODConsole
{
    void bar()
    {
        baz();
    }

};

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:

namespace TCODStatic
{
    IMPORT_FROM_TCOD(foo)
    IMPORT_FROM_TCOD(bar)
    IMPORT_FROM_TCOD(baz)
    IMPORT_FROM_TCOD(spaz)
}

and then you'll be able to write using namespace TCODStatic; anywhere you want.

葬心 2024-11-16 01:55:20

简短的回答:不使用宏就不行。 C++ 语言中没有适当的机制可以做这样的事情:

class A
{
public:
  static void foo();
};

// ...

using A;
foo();

您可以使用宏来为您构建这些表达式:

#define FOO() (A::foo())

FOO();

但是我不鼓励这样做,因为如果您广泛使用此类宏,它可能会成为维护噩梦。您最终会得到如下代码:

FOO();
BAR();
SUPERGIZMO(a, b, c);

...其中这些内容都没有在程序中的任何位置定义为一流名称。您已经失去了所有类型检查,代码使用了只有您知道的“秘密语言”,并且变得难以调试、扩展和修复。

编辑:

你可以写一种桥。像这样:

class FooBar
{
public: 
  static void gizmo();
};

namespace Foo
{
  void gizmo() { FooBar::gizmo(); };
};

using namespace Foo;

gizmo();

Short answer: not without using macros. There is no mechanism in the C++ language proper to do something like this:

class A
{
public:
  static void foo();
};

// ...

using A;
foo();

You could use a macro to build these expressions for you:

#define FOO() (A::foo())

FOO();

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:

FOO();
BAR();
SUPERGIZMO(a, b, c);

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

class FooBar
{
public: 
  static void gizmo();
};

namespace Foo
{
  void gizmo() { FooBar::gizmo(); };
};

using namespace Foo;

gizmo();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文