member functions as friends - friends of each other?

Discussion in 'C++' started by bipod.rafique@gmail.com, Jul 16, 2005.

  1. Guest

    Hello All,

    I have the following two classes:

    class testb{
    private:
    int b;
    public:
    void friend_of_testa();
    };

    class testa{
    friend void testb::friend_of_testa();
    private:
    int a;
    };

    Here, testb's member function friend_of_testa() is a friend of testa.
    So private member of testa (say int a), is accessible from testb's
    friend_of_testa. This is all good.

    But what if I wanted a member function of testa be a friend of class
    testb as well? like the following:

    class testa; //forward reference

    class testb{
    friend void testa::friend_of_testb();
    private:
    int b;
    public:
    void friend_of_testa();
    };

    class testa{
    friend void testb::friend_of_testa();

    private:
    int a;
    public:
    void friend_of_testb();
    };

    This is not possible as testa's friend_of_testb() is unknown to the
    compiler at the time of testb's class declaration.

    I know I would need a forward reference for testa before testb can use
    it. But how do make testa's friend_of_testb() a forword reference for
    the above to work?

    Thanks
    Bipod
    , Jul 16, 2005
    #1
    1. Advertising

  2. wrote:
    > Hello All,
    >
    > I have the following two classes:
    >
    > class testb{
    > private:
    > int b;
    > public:
    > void friend_of_testa();
    > };
    >
    > class testa{
    > friend void testb::friend_of_testa();
    > private:
    > int a;
    > };
    >
    > Here, testb's member function friend_of_testa() is a friend of testa.
    > So private member of testa (say int a), is accessible from testb's
    > friend_of_testa. This is all good.
    >
    > But what if I wanted a member function of testa be a friend of class
    > testb as well? like the following:
    >
    > class testa; //forward reference
    >
    > class testb{
    > friend void testa::friend_of_testb();
    > private:
    > int b;
    > public:
    > void friend_of_testa();
    > };
    >
    > class testa{
    > friend void testb::friend_of_testa();
    >
    > private:
    > int a;
    > public:
    > void friend_of_testb();
    > };
    >
    > This is not possible as testa's friend_of_testb() is unknown to the
    > compiler at the time of testb's class declaration.


    Of course.

    > I know I would need a forward reference for testa before testb can use
    > it. But how do make testa's friend_of_testb() a forword reference for
    > the above to work?


    You cannot, because there is no way to forward-declare member
    functions. The only thing you can do is :

    class testa;

    class testb
    {
    friend testa;

    public:
    void f();
    };

    class testa
    {
    friend testb::testa;
    public:
    void g();
    };

    testb::f() is a friend of testa and testa is a friend of testb. That's
    the closest you can get.


    Jonathan
    Jonathan Mcdougall, Jul 16, 2005
    #2
    1. Advertising

  3. Guest

    yeah I guess so...

    As an alternative, I can make the whole class as friend and vice versa.
    That works fine.

    Thanks
    , Jul 16, 2005
    #3
    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. Guenther Sohler
    Replies:
    6
    Views:
    1,040
    anthius
    Oct 7, 2006
  2. Replies:
    0
    Views:
    646
  3. Dan Smithers

    private member functions as friends

    Dan Smithers, Jul 7, 2008, in forum: C++
    Replies:
    3
    Views:
    581
    Dan Smithers
    Jul 9, 2008
  4. Hicham Mouline
    Replies:
    0
    Views:
    423
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    405
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page