c++ - 在析构函数中使用 `friend` 说明符的示例?

标签 c++ language-lawyer destructor friend

[class.dtor]/1包含以下句子:

Each decl-specifier of the decl-specifier-seq of a destructor declaration (if any) shall be friend, inline, or virtual.



我真的很想看到一个使用带有 friend 的析构函数的示例。说明符。

最佳答案

假设您要允许 A 类的私有(private)成员用于类B .没问题,你声明B作为里面的 friend A .

进一步假设您希望将使用限制为 B的析构函数而已。因此,您只需声明 B的析构函数作为 friend :

struct A
{
private:
    // some private stuff
    friend B::~B();
};

this example on ideone .

关于c++ - 在析构函数中使用 `friend` 说明符的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49650224/

相关文章:

c++ - 如何使用 {"data": "0001"}' 输入声明 char *

c++ - "constructing"一个可以用 memcpy 简单复制的对象

c++ - 构造函数在 g++ 和 clang++ 中委托(delegate)给自己

c++ - C++ 声明中的显式限定

c++ - 在 C++ 类的构造函数中引发异常

c++ - 与指向数组的指针 union

c++ - Pugixml C++解析XML

c# - 为什么在这个非常简单的场景中我的 .net 析构函数没有被调用?

c++ - 为什么不能从析构函数中抛出。例子

C++迭代器问题