Connecting Tech Pros Worldwide Help | Site Map

friend class problem

  #1  
Old July 22nd, 2005, 06:10 AM
chandra sekhar
Guest
 
Posts: n/a
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

  #2  
Old July 22nd, 2005, 06:11 AM
Karl Heinz Buchegger
Guest
 
Posts: n/a

re: friend class problem


chandra sekhar wrote:[color=blue]
>
> 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..[/color]

Why?
The Frame class has a public interface. You ought to use that, that's why
it exists.
[color=blue]
>
> class myView : public View
> {
> public:
> void print_1()
> {
> myFrame->printFramemsg();[/color]

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

View::print();


--
Karl Heinz Buchegger
kbuchegg@gascad.at
  #3  
Old July 22nd, 2005, 06:11 AM
Ernst Murnleitner
Guest
 
Posts: n/a

re: friend class problem


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


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested friend class in nested template problem tonvandenheuvel@gmail.com answers 3 December 7th, 2007 03:05 PM
friend sibling class nested in derived class problem in gcc 4.0 Tomas Sieger answers 1 December 20th, 2005 09:05 AM
problems with friend class in templates Joe Carner via .NET 247 answers 1 November 17th, 2005 04:47 PM
Friend class and typedef problem Alex Borghgraef answers 1 July 22nd, 2005 07:31 AM