Page 116 - DCAP404 _Object Oriented Programming
P. 116

Unit 5: Static Members




          };                                                                                    Notes
          class  C  {
              void  f(A*  p)  {
          //    p->a = 2;
            }
          };
          The compiler would not allow the statement p->a = 2 because class C is not a friend of class A,
          although C is a friend of A.
          If you declare a friend in a local class, and the friend’s name is unqualified, the compiler will
          look for the name only within the innermost enclosing nonclass scope. You  must declare  a
          function before declaring it as a friend of a local scope. You do not have to do so with classes.





             Notes  Note that a declaration of a friend class will hide a class in an enclosing scope with
             the same name.

          The following example demonstrates this:

          class  X  {  };
          void  a();
          void  f()  {
             class  Y {  };
              void  b();
            class  A {

                  friend  class  X;
                  friend  class  Y;
                  friend  class  Z;
          //        friend  void  a();
                  friend  void  b();
          //        friend  void  c();
            };
              ::X  moocow;
          //    X  moocow2;

          }
          In the above example, the compiler will allow the following statements:
          1.   friend class X: This statement does not declare ::X as a friend of A, but the local class X as a
               friend, even though this class is not otherwise declared.

          2.   friend class Y: Local class Y has been declared in the scope of f().
          3.   friend class Z: This statement declares the local class Z as a friend of A even though Z is not
               otherwise declared.




                                           LOVELY PROFESSIONAL UNIVERSITY                                   109
   111   112   113   114   115   116   117   118   119   120   121