friend classes and functions

Discussion in 'C++' started by Jess, May 20, 2007.

  1. Jess

    Jess Guest

    Hello,

    I am trying to define a friend function for my class as follows:

    #include<iostream>

    using namespace std;

    class B;

    class A{
    friend void B::g(const A&);
    //friend class B;
    private:
    int x;
    public:
    A(int a):x(a){}
    };

    class B:public A{
    public:
    B(int a):A(a){}
    void g(const A& a){cout << a.x << endl;}
    };

    However, this failed with compiler error saying A::x is private and
    member B::g is declared before type B is defined. This is surprising
    because I've given forward declaration for B. I also declared B::g as
    a friend, and so I think it should be able to access A::x. What is
    going wrong here?

    I then tried to replace friend void B::g(const A&) with friend class B
    and then it worked. However, I'd only like B's g method to access A's
    private part, not the whole B class. Is there a solution for my
    problem?

    Thanks,
    Jess
     
    Jess, May 20, 2007
    #1
    1. Advertising

  2. Jess wrote:
    > Hello,
    >
    > I am trying to define a friend function for my class as follows:
    >
    > #include<iostream>
    >
    > using namespace std;
    >
    > class B;
    >
    > class A{
    > friend void B::g(const A&);
    > //friend class B;
    > private:
    > int x;
    > public:
    > A(int a):x(a){}
    > };
    >
    > class B:public A{
    > public:
    > B(int a):A(a){}
    > void g(const A& a){cout << a.x << endl;}
    > };
    >
    > However, this failed with compiler error saying A::x is private and
    > member B::g is declared before type B is defined. This is surprising
    > because I've given forward declaration for B. I also declared B::g as
    > a friend, and so I think it should be able to access A::x. What is
    > going wrong here?


    You cannot make a member function a friend based on only a forward
    declaration of the class.

    >
    > I then tried to replace friend void B::g(const A&) with friend class B
    > and then it worked. However, I'd only like B's g method to access A's
    > private part, not the whole B class. Is there a solution for my
    > problem?


    No, but what is the problem exactly? Friendship of individual member
    functions is fairly pointless. Since you are writing both classes,
    whether they are friends of each other of not is only of concern to you.
    It makes no difference at all to the users of your class, it's only an
    implementation detail for your class. So just declare B to be a friend of A.


    >
    > Thanks,
    > Jess
    >


    john
     
    John Harrison, May 20, 2007
    #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. Razvan
    Replies:
    11
    Views:
    923
    Andrew Thompson
    Jul 17, 2004
  2. Adam Parkin
    Replies:
    6
    Views:
    520
    Victor Bazarov
    Apr 24, 2004
  3. mojmir
    Replies:
    3
    Views:
    337
    mojmir
    Sep 12, 2008
  4. SunRaySon

    Friend functions or classes

    SunRaySon, Dec 2, 2007, in forum: Ruby
    Replies:
    4
    Views:
    131
    Ilan Berci
    Dec 3, 2007
  5. Peter
    Replies:
    2
    Views:
    278
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page