c++ - 友元函数和类的定义

标签 c++ class friend

为什么可以定义友元函数

class CL
{
    friend void fnc(){}
};

但是友元类不行吗?

class CL
{
    friend class FriendClass{}; //error
};

最佳答案

内部类是它在其中声明的类的友元。

friend 说明符做这件事是没有意义的,因为它是一个内部类,可以私有(private)访问外部类。

而且,标准正式地说:

[class.friend]/p2

A class shall not be defined in a friend declaration

关于c++ - 友元函数和类的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26162679/

相关文章:

java - 当数据存储和数据格式在未来版本中可能发生变化时,存储应用程序数据的最佳方式?

Python面向对象编程: Automatically changing instances of a class after changing a class attribute?

c++ - 在 D 中使用 C++ 类

c++ - 在 C++ 中,使用内联说明符的正确方法是什么?

c++ - 自相矛盾的错误消息 - 运算符重载 <<

c++ - 如何在 C++ 中使用/访问类的内部友元函数?

c# - 如何克服 C# 中缺乏友元的问题?

c++ - streambuf::xsgetn 和状态标志

c++ - const WCHAR * myVar 与 const char * myVar

c++ - 从 vector 中删除一对并为 vector 对保留空间