我认为我可以这样做:
class TestA
{
private:
class Nested
{
};
};
class TestB
{
public:
friend class TestA;
friend class TestA::Nested;
};
但我得到一个错误:
Error C2248 'TestA::Nested': cannot access private class declared in class
有没有办法让私有(private)嵌套类成为 friend ?我该怎么做?
我在尝试在 MSVC 2017 (C++17) 中编译 MSVC 6 项目时遇到此错误。我想它当时有效。
最佳答案
与您访问任何其他私有(private)事物的方式相同。你需要另一种友元:
class TestA
{
friend class TestB; // <== this
private:
class Nested
{
};
};
class TestB
{
public:
friend class TestA;
friend class TestA::Nested; // <== now we're a friend of TestA, so we can access it
};
关于c++ - 如何与私有(private)嵌套类成为 friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56950798/