gpt4 book ai didi

c++ - 我可以在 C++ 类中声明 "using namespace"吗?

转载 作者:IT老高 更新时间:2023-10-28 23:01:45 26 4
gpt4 key购买 nike

假设有一个 C++ 类。还有一个命名空间,应该只在我的类(class)内可见。该怎么办?

class SomeClass
{
using namespace SomeSpace;

public:
void Method1();
void Method2();
void Method3();
};

namespace SomeSpace
{
/*some code*/
};

最佳答案

using namespace X; 被称为 using directive它只能出现在命名空间和函数范围内,不能出现在类范围内。因此,您尝试做的事情在 C++ 中是不可能的。最好的办法是在该类的命名空间范围内编写 using 指令,这可能是不可取的。

不过,再想一想,分析你的话,

Assume having a C++ class. And there's a namespace which should bevisible only inside my class. What to do for that?

我会建议类似以下的内容,但我不确定这是否是您想要的。

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
};

};

关于c++ - 我可以在 C++ 类中声明 "using namespace"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9296215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com