如何在命名空间中使用 extern 类?
How can I extern classes in a namespace?
我想在命名空间中 extern classes,而不是再次定义整个 class。例如,我有 class A
:
class A
{
private:
int value;
public:
A(int value);
int get_value();
};
和class B
:
class B
{
private:
int value;
public:
B(int value);
int get_value();
};
但我想将 A
和 B
class 外置,而不需要再次在命名空间中定义它们,例如:
#include "a.hpp"
#include "b.hpp"
namespace kc
{
extern class A;
extern class B;
}
我不想做:
namespace kc
{
class A
{
private:
int value;
public:
A(int value);
int get_value();
};
class B
{
private:
int value;
public:
B(int value);
int get_value();
};
}
如果您想引用相同的 类 就好像它们是命名空间的成员一样,那么您可以使用一对 using 声明来实现。
namespace kc
{
using ::A;
using ::B;
};
但是请注意,不会使 类 成为命名空间的成员,ADL 等语言功能不会受到它的影响。
我想在命名空间中 extern classes,而不是再次定义整个 class。例如,我有 class A
:
class A
{
private:
int value;
public:
A(int value);
int get_value();
};
和class B
:
class B
{
private:
int value;
public:
B(int value);
int get_value();
};
但我想将 A
和 B
class 外置,而不需要再次在命名空间中定义它们,例如:
#include "a.hpp"
#include "b.hpp"
namespace kc
{
extern class A;
extern class B;
}
我不想做:
namespace kc
{
class A
{
private:
int value;
public:
A(int value);
int get_value();
};
class B
{
private:
int value;
public:
B(int value);
int get_value();
};
}
如果您想引用相同的 类 就好像它们是命名空间的成员一样,那么您可以使用一对 using 声明来实现。
namespace kc
{
using ::A;
using ::B;
};
但是请注意,不会使 类 成为命名空间的成员,ADL 等语言功能不会受到它的影响。