friend class problem

Discussion in 'C++' started by chandra sekhar, Jan 13, 2004.

  1. Hi All,

    I have problem regarding the friend class.
    I have a library where the class are declration is follows ( only
    example )

    class Frame
    {
    public:
    protected:
    void printFramemsg()
    {
    cout << " test... " << endl;
    }
    private:

    // by making this we can access the protected members in the view

    friend class View;
    };

    class View
    {
    public:
    void setFrame(Frame* aFrame)
    {
    myFrame = aFrame;
    }
    void print()
    {
    myFrame->printFramemsg();
    }
    protected:
    Frame* myFrame;
    };

    Now I want to derive the View class and access the protected and private
    members of the Frame class..

    class myView : public View
    {
    public:
    void print_1()
    {
    myFrame->printFramemsg();
    }
    private:
    };

    Is any workarround or other way to do this .. ( Here I do not want to
    derive a class from Frame ... )

    Bye
    Chandra
     
    chandra sekhar, Jan 13, 2004
    #1
    1. Advertising

  2. chandra sekhar wrote:
    >
    > class View
    > {
    > public:
    > void setFrame(Frame* aFrame)
    > {
    > myFrame = aFrame;
    > }
    > void print()
    > {
    > myFrame->printFramemsg();
    > }
    > protected:
    > Frame* myFrame;
    > };
    >
    > Now I want to derive the View class and access the protected and private members of the Frame
    > class..


    Why?
    The Frame class has a public interface. You ought to use that, that's why
    it exists.

    >
    > class myView : public View
    > {
    > public:
    > void print_1()
    > {
    > myFrame->printFramemsg();


    myView is not a friend of the frame. But View (the base class of myView)
    is. Thus:

    View::print();


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 13, 2004
    #2
    1. Advertising

  3. Dear Chandra,

    There is no way for inheritance of friendship. So, you have to make the
    derived class a also a friend.
    With private members there is no other way.
    But with protected members I don not know, as I never had to use friends.

    I would derive a class MyFrame from Frame. Here you can access the protected
    member of Frame. Make the function for accessing public and you need no
    friends - or make it protected or private and make MyView a friend of
    MyFrame.

    Greetings
    Ernst
     
    Ernst Murnleitner, Jan 13, 2004
    #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. Alex Vinokur
    Replies:
    1
    Views:
    379
    mlimber
    Jun 6, 2006
  2. Jim Langston
    Replies:
    6
    Views:
    339
    Jim Langston
    Aug 22, 2006
  3. request@no_spam.com
    Replies:
    5
    Views:
    458
  4. Hicham Mouline
    Replies:
    0
    Views:
    348
    Hicham Mouline
    Nov 11, 2008
  5. Peter
    Replies:
    2
    Views:
    310
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page