Thread: friend function and friend classes...usage question??

  1. #1
    Registered User actionbasti's Avatar
    Join Date
    Dec 2002

    friend function and friend classes...usage question??

    My question is to get a better understanding of C++ usage, or the frequenz of using different aspects of the language.

    Are friend functions/classes used a lot in C++ OOP. Since one classe cannot communicate with the private functions and members of another class, is friend one of the most used keyword to allow that or are there other ways... what do programers do in that case.

    I am asking because my professor told us that we wont need friend functions/classes much in the real world. however I have the feeling that they are more important than he told us!!!!


  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    I guess you could call where I work "the real world", and I rarely (if ever) use friend classes or functions. I know there are some cases where they are preferred, but in general I think they are a bad way to get around the access rules enforced by private, protected, and public. That is probably why he is discouraging there use, since some programmers attempt to use them as an easy way to solve a problem instead of using better design techniques.

    The point of a class is to encapsulate the inner workings of the object and only provide an interface so that other code can use that object. Using friends defeats that purpose. If a programmer wants one class to see the privates of another class, then they usually rethink their design.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Agreed. You really should never have to use friend classes/functions. If you find that your class design requires a friend function or class then perhaps you should rethink the design of your class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2006, 05:56 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Friend function
    By Verdagon in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 11:44 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM