Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import nested classes into namespace - C++

Tags:

Say I have a class like this:

class A { public:     class B {         // ...     };     static void f();     // ... }; 

I can refer to B as A::B and to f() as A::f(), but can I import B and f() into the global/current namespace? I tried

using A::B; 

but that gave me a compilation error.

like image 291
Baruch Avatar asked Jul 25 '12 10:07

Baruch


1 Answers

Here are two workarounds for your problem:

1) Class B:

typedef A::B B; 

2) Function f():

inline void f() {     A::f(); } 

But think twice before using them.

Edit: In C++11 you can do auto f = A::f; but this actually creates a pointer to a function and functions pointers cannot be inlined.

like image 137
Sergey K. Avatar answered Oct 04 '22 15:10

Sergey K.