Page 114 - DCAP404 _Object Oriented Programming
P. 114

Unit 5: Static Members




          {                                                                                     Notes
                int  iMsgNo  =  1;  //  assign  arbitrary  value  as  message  number
                strcpy(  BufferedIO::BufferedInput::message,
                         rgszMessage[iMsgNo] );
                return  iMsgNo;
          }
          int  main()

          {
          }
          The preceding code shows the function GetExtendedErrorStatus declared as a friend function. In
          the function, which is defined in file scope, a message is copied from a static array into a class
          member. Note that a better implementation ofGetExtendedErrorStatus is to declare it as:
          int GetExtendedErrorStatus( char *message )
          With the preceding interface, several classes can use the services of this function by passing a
          memory location where they want the error message copied.




              Task  The friend function has access to the private data member of the Point object it
             receives as a parameter. Analyse.

          5.4.3 Friend Function in Local Classes

          The name of a friend function or class first introduced in a friend declaration is not in the scope
          of the class granting friendship (also called the enclosing class) and is not a member of the class
          granting friendship.
          The name of a function first introduced in a friend declaration is in the scope of the first nonclass
          scope that contains the enclosing class. The body of a function provided in a friend declaration
          is handled in the same way as a member function defined within  a class. Processing of the
          definition does not start until the end of the outermost enclosing class. In addition, unqualified
          names in the body of the function definition are searched for starting from the class containing
          the function definition.
          If the name of a friend class has been introduced before the friend declaration, the compiler
          searches for a class name that matches the name of the friend class beginning at the scope of the
          friend declaration. If the declaration of a nested class is followed by the declaration of a friend
          class with the same name, the nested class is a friend of the enclosing class.
          The scope of a friend class name is the first nonclass enclosing scope. For example:
          class  A  {
                class  B  {  //  arbitrary  nested  class  definitions
                friend class C;
             };

          };
          is  equivalent  to:



                                           LOVELY PROFESSIONAL UNIVERSITY                                   107
   109   110   111   112   113   114   115   116   117   118   119