c++ - 如何与私有嵌套类成为朋友
问题描述
我以为我可以这样做:
class TestA
{
private:
class Nested
{
};
};
class TestB
{
public:
friend class TestA;
friend class TestA::Nested;
};
但我收到一个错误:
错误 C2248 'TestA::Nested':无法访问在类中声明的私有类
有没有办法与私有嵌套类成为朋友?我该怎么做?
尝试在 MSVC 2017 (C++17) 中编译 MSVC 6 项目时遇到此错误。我想它当时奏效了。
解决方案
以同样的方式访问任何其他私人事物。你需要友谊的另一种方式:
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
};
推荐阅读
- java - 在 ruby 项目中添加 jruby 后构建失败
- c# - 无法使用 C# 中的 Outlook Graph API v2 从电子邮件中读取附件
- firebase-cloud-messaging - 如何识别和删除服务器上过期的 FCM 令牌?
- python - 这些命令是否执行相同的操作?
- java - 如何在命令行上为java的modulepath指定多个jar文件?
- elasticsearch - 使用 Fluentd 配置 Istio 会引发超时错误
- oracle - Kafka JDBC 接收器连接器 - Oracle
- php - Homebrew 升级丢弃 PHP:dyld:未加载库:/usr/local/opt/libpsl/lib/libpsl.5.dylib
- python - 从 DM 中的回复中获取消息内容
- node.js - 无法升级node js最新版本