假设有一个C++类.还有一个名称空间,只有在我的班级内才能看到.该怎么办?
class SomeClass
{
using namespace SomeSpace;
public:
void Method1();
void Method2();
void Method3();
};
namespace SomeSpace
{
/*some code*/
};
Run Code Online (Sandbox Code Playgroud)
using namespace X;被称为using指令,它只能出现在命名空间和函数范围内,而不能出现在类范围内.所以你想要做的事情在C++中是不可能的.您可以做的最好的事情是在该类的命名空间范围内编写using指令,这可能是不可取的.
然而,在第二个想法,分析你的话,
假设有一个C++类.还有一个名称空间,只有在我的班级内才能看到.该怎么办?
我建议如下,我不确定你想要什么.
class A
{
public:
void Method1();
void Method2();
void Method3();
private:
class B
{
//public static functions here, instead of namespace-scope
// freestanding functions.
//these functions will be accessible from class A(and its friends, if any)
//because B is private to A
};
};
Run Code Online (Sandbox Code Playgroud)