1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

derived class is friend class to base class

Discussion in 'C' started by sharmila, Apr 20, 2007.

  1. sharmila

    sharmila New Member

    Mar 24, 2006
    Likes Received:
    Trophy Points:
    Hi all,
    I saw a code in which derived class is friend class of base class. I did not understand it's purpose. We can access all the fun.s and var.s of base class in derived class (since it is friend to base), if we want the total control of base then why can't we make every thing as protected , instead of saying derived is friend to base?

    class Base {
      // code
      friend class Derived1;
      friend class Derived2;
    class Derived1 : public Base {
    // code
    class Derived2 : public Base {
    // code
    I thought may be for other derived class it should not give total control, and so they made derived as friend, but this base class is inherited by two derived classes and both are friends to base. No other class is derived from this base. Can you explain me when can we use the concept of derived class is friend to base class ( in real time)?

    and one more doubt is how friend is implemented internally? means how it is represented in object table?

    Last edited by a moderator: Apr 20, 2007

Share This Page