friend function accessing protected members of base class

Discussion in 'C++' started by Rahul, Apr 7, 2008.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I have the following code,

    class sample
    {
    protected : int i;
    private: int j;
    public: sample()
    {
    i = 5;
    j = 10;
    }
    };

    class two : public sample
    {
    public: two()
    {
    i = 10;
    }
    friend void test()
    {
    i = 111;
    }
    };

    And i get an error saying test() cannot access two::sample::i;
    Is there any was for the friend function test() to access the
    protected members?

    Thanks in advance ! ! !
    Rahul, Apr 7, 2008
    #1
    1. Advertising

  2. Rahul

    Rahul Guest

    On Apr 7, 5:56 pm, Rahul <> wrote:
    > Hi Everyone,
    >
    > I have the following code,
    >
    > class sample
    > {
    > protected : int i;
    > private: int j;
    > public: sample()
    > {
    > i = 5;
    > j = 10;
    > }
    > };
    >
    > class two : public sample
    > {
    > public: two()
    > {
    > i = 10;
    > }
    > friend void test()
    > {
    > i = 111;
    > }
    > };
    >
    > And i get an error saying test() cannot access two::sample::i;
    > Is there any was for the friend function test() to access the
    > protected members?
    >
    > Thanks in advance ! ! !


    oops, not doing the write way, found the mistake

    friend void test()
    {
    two obj;
    obj.i = 111;
    }
    Rahul, Apr 7, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Suzanne Vogel
    Replies:
    5
    Views:
    2,354
    Dan W.
    Dec 9, 2003
  2. Replies:
    2
    Views:
    808
    Noah Roberts
    Jun 9, 2006
  3. flopbucket
    Replies:
    2
    Views:
    424
    Andrey Tarasevich
    Jun 23, 2006
  4. Replies:
    4
    Views:
    508
    James Kanze
    Oct 23, 2007
  5. Bhawna
    Replies:
    7
    Views:
    474
    Bhawna
    Aug 26, 2008
Loading...

Share This Page