friend class problem

C

chandra sekhar

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
 
K

Karl Heinz Buchegger

chandra said:
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();
 
E

Ernst Murnleitner

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top