Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to declare a friend function that can access two class

Reply
Thread Tools

how to declare a friend function that can access two class

 
 
freegnu
Guest
Posts: n/a
 
      10-23-2006
how to declare a friend function that can access two class
it will look like the following
class A
{
private:
int i;
public:
A(){}
~A(){}
friend void call(A &a, B &b);

};

class A
{
private:
int j;
public:
B();
~B()
friend void call(A &a, B &b);
};

void call(A &a, B &b)
{
cout << a.i << endl << b.j <<endl;
}
 
Reply With Quote
 
 
 
 
Frank-O
Guest
Posts: n/a
 
      10-23-2006
A friend function can be a member of another class :
class A;

class B
{
private :
int j;
public :
B(int j_):j(j_){}

void call(A&);
};


class A
{
private :
int i;
public :
A(int i_):i(i_){}
friend void B::call(A &a);
};


void B::call(A &a)
{

cout << a.i << j << endl;
}

or

class A;

class B
{
private :
int j;
public :
B(int j_):j(j_){}

friend void call(A&,B&b);
};


class A
{
private :
int i;
public :
A(int i_):i(i_){}
friend void call(A &a,B &b);
};


void call(A &a,B &b)
{

cout << a.i << b.j << endl;
}

freegnu wrote:
> how to declare a friend function that can access two class
> it will look like the following
> class A
> {
> private:
> int i;
> public:
> A(){}
> ~A(){}
> friend void call(A &a, B &b);
>
> };
>
> class A
> {
> private:
> int j;
> public:
> B();
> ~B()
> friend void call(A &a, B &b);
> };
>
> void call(A &a, B &b)
> {
> cout << a.i << endl << b.j <<endl;
> }
> ------=_NextPart_000_0045_01C6F6B8.F8416B10
> Content-Type: text/html; charset=gb2312
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 2408
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=Content-Type content="text/html; charset=gb2312">
> <META content="MSHTML 6.00.2900.2963" name=GENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY bgColor=#ffffff>
> <DIV><FONT size=2>
> <DIV class=altbg2 id=code0
> style="CLEAR: both; BORDER-RIGHT: #698cc3 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: #698cc3 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: #698cc3 1px solid; WORD-BREAK: break-all; PADDING-TOP: 5px; BORDER-BOTTOM: #698cc3 1px solid; FONT-FAMILY: fixedsys"><FONT
> face=>how to declare a friend function that can access two class</FONT></DIV>
> <DIV class=altbg2
> style="CLEAR: both; BORDER-RIGHT: #698cc3 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: #698cc3 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: #698cc3 1px solid; WORD-BREAK: break-all; PADDING-TOP: 5px; BORDER-BOTTOM: #698cc3 1px solid; FONT-FAMILY: fixedsys"><FONT
> face=>it will look like the following</FONT></DIV>
> <DIV class=altbg2
> style="CLEAR: both; BORDER-RIGHT: #698cc3 1px solid; PADDING-RIGHT: 10px; BORDER-TOP: #698cc3 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 3px 2em 2em; BORDER-LEFT: #698cc3 1px solid; WORD-BREAK: break-all; PADDING-TOP: 5px; BORDER-BOTTOM: #698cc3 1px solid; FONT-FAMILY: fixedsys">class
> A<BR>{<BR>&nbsp;&nbsp;private:<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp;int i;<BR>&nbsp;&nbsp;public:<BR>&nbsp; &nbsp;&nbsp;
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;A(){}<BR>&nbsp; &nbsp;&nbsp;
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;~A(){}<BR>&nbsp; &nbsp;&nbsp;
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; friend void call(A &amp;a, B
> &amp;b);<BR><BR>};<BR><BR>class A<BR>{<BR>&nbsp;&nbsp;private:<BR>&nbsp;
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int
> j;<BR>&nbsp;&nbsp;public:<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
> &nbsp; B();<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
> ~B()<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; friend void call(A
> &amp;a, B &amp;b);<BR>};<BR><BR>void call(A &amp;a, B
> &amp;b)<BR>{<BR>&nbsp;&nbsp;cout &lt;&lt; a.i &lt;&lt; endl &lt;&lt; b.j
> &lt;&lt;endl;<BR>}</DIV></FONT></DIV></BODY></HTML>
>
> ------=_NextPart_000_0045_01C6F6B8.F8416B10--


 
Reply With Quote
 
 
 
 
freegnu
Guest
Posts: n/a
 
      10-23-2006
thanks for your reply
"Frank-O" <(E-Mail Removed)>
??????:(E-Mail Removed) groups.com...
A friend function can be a member of another class :
class A;

class B
{
private :
int j;
public :
B(int j_):j(j_){}

void call(A&);
};


class A
{
private :
int i;
public :
A(int i_):i(i_){}
friend void B::call(A &a);
};


void B::call(A &a)
{

cout << a.i << j << endl;
}

or

class A;

class B
{
private :
int j;
public :
B(int j_):j(j_){}

friend void call(A&,B&b);
};


class A
{
private :
int i;
public :
A(int i_):i(i_){}
friend void call(A &a,B &b);
};


void call(A &a,B &b)
{

cout << a.i << b.j << endl;
}

freegnu wrote:
> how to declare a friend function that can access two class
> it will look like the following
> class A
> {
> private:
> int i;
> public:
> A(){}
> ~A(){}
> friend void call(A &a, B &b);
>
> };
>
> class A
> {
> private:
> int j;
> public:
> B();
> ~B()
> friend void call(A &a, B &b);
> };
>
> void call(A &a, B &b)
> {
> cout << a.i << endl << b.j <<endl;
> }
> ------=_NextPart_000_0045_01C6F6B8.F8416B10
> Content-Type: text/html; charset=gb2312
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 2408
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=Content-Type content="text/html; charset=gb2312">
> <META content="MSHTML 6.00.2900.2963" name=GENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY bgColor=#ffffff>
> <DIV><FONT size=2>
> <DIV class=altbg2 id=code0
> style="CLEAR: both; BORDER-RIGHT: #698cc3 1px solid; PADDING-RIGHT: 10px;
> BORDER-TOP: #698cc3 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px;
> MARGIN: 3px 2em 2em; BORDER-LEFT: #698cc3 1px solid; WORD-BREAK:
> break-all; PADDING-TOP: 5px; BORDER-BOTTOM: #698cc3 1px solid;
> FONT-FAMILY: fixedsys"><FONT
> face=>how to declare a friend function that can access two
> class</FONT></DIV>
> <DIV class=altbg2
> style="CLEAR: both; BORDER-RIGHT: #698cc3 1px solid; PADDING-RIGHT: 10px;
> BORDER-TOP: #698cc3 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px;
> MARGIN: 3px 2em 2em; BORDER-LEFT: #698cc3 1px solid; WORD-BREAK:
> break-all; PADDING-TOP: 5px; BORDER-BOTTOM: #698cc3 1px solid;
> FONT-FAMILY: fixedsys"><FONT
> face=>it will look like the following</FONT></DIV>
> <DIV class=altbg2
> style="CLEAR: both; BORDER-RIGHT: #698cc3 1px solid; PADDING-RIGHT: 10px;
> BORDER-TOP: #698cc3 1px solid; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px;
> MARGIN: 3px 2em 2em; BORDER-LEFT: #698cc3 1px solid; WORD-BREAK:
> break-all; PADDING-TOP: 5px; BORDER-BOTTOM: #698cc3 1px solid;
> FONT-FAMILY: fixedsys">class
> A<BR>{<BR>&nbsp;&nbsp;private:<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
> &nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp;int i;<BR>&nbsp;&nbsp;public:<BR>&nbsp; &nbsp;&nbsp;
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;A(){}<BR>&nbsp; &nbsp;&nbsp;
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;~A(){}<BR>&nbsp; &nbsp;&nbsp;
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; friend void call(A &amp;a, B
> &amp;b);<BR><BR>};<BR><BR>class A<BR>{<BR>&nbsp;&nbsp;private:<BR>&nbsp;
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;int
> j;<BR>&nbsp;&nbsp;public:<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
> &nbsp; B();<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
> ~B()<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; friend void
> call(A
> &amp;a, B &amp;b);<BR>};<BR><BR>void call(A &amp;a, B
> &amp;b)<BR>{<BR>&nbsp;&nbsp;cout &lt;&lt; a.i &lt;&lt; endl &lt;&lt; b.j
> &lt;&lt;endl;<BR>}</DIV></FONT></DIV></BODY></HTML>
>
> ------=_NextPart_000_0045_01C6F6B8.F8416B10--



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Declare a Friend function which is a template member function of adifferent class getrakesh@gmail.com C++ 2 02-26-2008 11:32 AM
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= C++ 10 08-01-2007 01:48 AM
forward declare member function so that it can be friend function yancheng.cheok@gmail.com C++ 1 11-09-2006 11:30 AM



Advertisments