Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An example of the use of a `friend` specifier in a destructor?

[class.dtor]/1 contains the following sentence:

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

I'm really interested in seeing an example of the use of a destructor with a friend specifier.

like image 292
Alexander Avatar asked Apr 04 '18 11:04

Alexander


Video Answer


1 Answers

Suppose you want to allow private members of class A to be used in class B. No problem, you declare B as a friend inside A.

Suppose further that you want to restrict the usage to B's destructor only. Thus, you only declare B's destructor as a friend:

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

See this example on ideone.

like image 178
lisyarus Avatar answered Sep 19 '22 17:09

lisyarus