R K*_*R K 2 c++ namespaces friend forward-declaration c++17
考虑以下代码:
namespace A
{
class B
{
protected:
friend class C;
static void foo();
};
}
class C
{
public:
C() { A::B::foo(); }
};
int main()
{
C c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
按照当前构造,此代码将无法编译 - 中 声明的友谊class B适用于 a (当前不存在)A::C,而不是C全局命名空间中的 the 。假设我无法添加C到非全局命名空间,我如何有效地解决这个问题?我尝试过使用friend class ::C;,但编译器不喜欢这样。我也尝试过在范围class C;之前进行向前声明namespace A,但这似乎也不起作用。
添加类的前向声明C对我有用,您使用什么编译器?
class C;
namespace A
{
class B
{
protected:
friend class ::C;
static void foo();
};
}
// ...
Run Code Online (Sandbox Code Playgroud)
编辑:正如弗拉德指出的那样,只要您有适当的前向声明,两者friend C都friend ::C可以工作。但friend class C事实并非如此,我会将其转交给语言律师。
| 归档时间: |
|
| 查看次数: |
424 次 |
| 最近记录: |