When should you use 'friend' in C++?

前端 未结 30 2040
孤街浪徒
孤街浪徒 2020-11-22 10:12

I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language.

30条回答
  •  故里飘歌
    2020-11-22 10:51

    Another use: friend (+ virtual inheritance) can be used to avoid deriving from a class (aka: "make a class underivable") => 1, 2

    From 2:

     class Fred;
    
     class FredBase {
     private:
       friend class Fred;
       FredBase() { }
     };
    
     class Fred : private virtual FredBase {
     public:
       ...
     }; 
    

提交回复
热议问题