Friend function

Is there any difference when a friend "function/class" is declared inside a class as public ,and when a friend "function/class" is declared as private?


Thanks !
*I removed my post here* my failure sorry..
there is no difference at all.
Last edited on
> Is there any difference when a friend "function/class" is declared inside a class as public ,
> and when a friend "function/class" is declared as private?

No.
The meaning of the friend declaration is the same whether the friend declaration appears in the private, protected or public portion of the class member-specification. - IS


If a friend function is declared and defined inside a non-local class, the function is implicitly inline and is in the lexical scope of the class. But is is not a member of the class and therefore not subject to access control.

If a friend function is only declared, but not defined inside the class, its scope is unaffected by the friend declaration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>

class A
{
    // these functions are in the lexical scope of the class
    public: friend int foo( A& a ) { std::cout << "foo in lexical scope of A\n" ; return ++a.i ; }
    private: friend int bar( A& a ) { std::cout << "bar in lexical scope of A\n" ; return ++a.i ; }
    
    // foobar in not the lexical scope of the class
    // it is in the same scope that the class appears in
    // in this case, at the global namespace scope
    private: friend int foobar( A& a ) ;
    
    private: int i = 0 ;
};

namespace B { class C ; }
int bazbar( B::C & c ) ;

namespace B
{
    class C
    {
        // these functions are in the lexical scope of the class
        public: friend int foo( C & c ) { std::cout << "foo in lexical scope of B::C\n" ; return ++c.i ; }
        private: friend int bar( C& c ) { std::cout << "bar in lexical scope of B::C\n" ; return ++c.i ; }
        
       // foobar in not the lexical scope of the class
       // here, it is in namespace B
        private: friend int foobar( B::C& c ) ;
        
       // bazbar in not the lexical scope of the class
       // it is in the global namespace
        private: friend int ::bazbar( B::C& c ) ;

        private: int i = 0 ;
    };
}

int foobar(A& a ) { std::cout << "foobar in the global unnamed namespace\n" ; return ++a.i ; }
int B::foobar( B::C & c ) { std::cout << "foobar in namespace B\n" ; return ++c.i ; }
int bazbar( B::C & c ) { std::cout << "bazbar in the global unnamed namespace\n" ; return ++c.i ; }

int main()
{
    A a ;

    //A::foo(a) ; // **** error: 'foo' is not a member of 'A'
    // ::foo(a) ; // *** error: there is no foo in the global namespace
    // foo is not a member of A, but it is within the lexical scope of A

    foo(a) ; // argument dependant look up 
    bar(a) ; // argument dependant look up
    
    foobar(a) ; // unqualified name lookup
    ::foobar(a) ; 
    
    B::C c ;
    
    foo(c) ; // argument dependant look up
    bar(c) ; // argument dependant look up
    
    foobar(c) ; // argument dependant look up
    ::B::foobar(c) ;  
    
    bazbar(c) ; // unqualified name lookup
    ::bazbar(c) ;
}

http://coliru.stacked-crooked.com/a/25be222087cab794
Last edited on
thanks for the detailed explanation!!
I was going through the following of a Polynomial class, but the program won't compile giving me error that the "Term" member is protected when defining
friend functions :

http://coliru.stacked-crooked.com/a/f2d55c8154b938bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Polynomial
{ // instances represent polynomials; e.g., 2x^7 - 8x^3 + 5x^2 + 9
    // INVARIANTS: _degree == _terms.begin()._exp;
    //             it1->_exp == it2->_exp  ==>  it1 == it2
    //             *this == ZERO  <==>  _degree == -1
    //                            <==>  _terms.size() == 0;
protected:
    struct Term
    {     //////////////////////////////////////////////////////////////////////////////
          ////////////////////////////   ***** moved ***** /////////////////////////////
          //////////////////////////////////////////////////////////////////////////////

        Term(double=0.0,unsigned=0);
        bool operator==(const Term&) const;
        bool operator!=(const Term&) const;
        bool operator<(const Term&) const;
        double _coef;
        unsigned _exp;
    };
    
    //////////////////////////////////////////////////////////////////////////////
    ////////////////////////////   ***** here ***** //////////////////////////////
    ////////////////////////////////////////////////////////////////////////////// 
    ////////////// note: these need to be friends of the Polynomial class ////////
    //////////////////////////////////////////////////////////////////////////////
    friend ostream& operator<<(ostream&, const Term&); // ***** added *****
    friend Term operator-(const Term&);
    friend Term derivative(const Term&);
    friend Term antiderivative(const Term&);
    friend Term abs(const Term&);
    //////////////////////////////////////////////////////////////////////////////

   // .... 

http://coliru.stacked-crooked.com/a/da265451b1dca81d
Topic archived. No new replies allowed.