Static functions of a directive class?

I am using an API that has many functions in a class named TCODConsoleas static functions. Now I thought that it was in the name space, so I wrote using namespace TCODConsole;. Then I found out that TCODConsoleit is not a namespace, but a class.

Is there a way to import these functions in the same way you would use using namespace?

+3
source share
4 answers

Although I may misunderstand the question, if skill reduction is the goal, typedefing, like the following, is the goal?

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

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

, TCODConsole , static member , -, :

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

, myStaticFun() MyClass::myStaticFun(). . , . - . . - :

class MyClass {
    static void fun();
};

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

// call it
fun();

. , .

+3

, , - , , , , ( TCODConsole -). , bar(), - Foo, baz(), TCODConsole . , Foo TCODConsole:

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

};

, .: (

Boost.PP( Boost Preprocessor - - Boost ), , , , "" , ( ). , "", ( ) :

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

using namespace TCODStatic; .

+2

: . ++ , - :

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

// ...

using A;
foo();

You can use a macro to create these expressions for you:

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

FOO();

I would be discouraged by this because it can be a nightmare to support, you use such macros extensively. You will get 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 have lost all type checks, the code uses a "secret language" that you only 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();
0
source

All Articles