gpt4 book ai didi

c++ - 如何与私有(private)嵌套类成为 friend

转载 作者:IT老高 更新时间:2023-10-28 22:25:02 25 4
gpt4 key购买 nike

我认为我可以这样做:

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/

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