how to declare a friend function that can access two class



 DEVELOP > c-Plus-Plus > how to declare a friend function that can access two class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "freegnu"
Date: 23 Oct 2006 02:36:12 AM
Object: how to declare a friend function that can access two class
This is a multi-part message in MIME format.
------=_NextPart_000_0045_01C6F6B8.F8416B10
Content-Type: text/plain;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.2963" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>
<DIV class=3Daltbg2 id=3Dcode0=20
style=3D"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=20
face=3D=CB=CE=CC=E5>how to declare a friend function that can access two =
class</FONT></DIV>
<DIV class=3Daltbg2=20
style=3D"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=20
face=3D=CB=CE=CC=E5>it will look like the following</FONT></DIV>
<DIV class=3Daltbg2=20
style=3D"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=20
A<BR>{<BR>&nbsp;&nbsp;private:<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; =
&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp;int i;<BR>&nbsp;&nbsp;public:<BR>&nbsp; &nbsp;&nbsp;=20
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;A(){}<BR>&nbsp; &nbsp;&nbsp; =
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;~A(){}<BR>&nbsp; =
&nbsp;&nbsp;=20
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; friend void call(A &amp;a, B=20
&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=20
j;<BR>&nbsp;&nbsp;public:<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; =
&nbsp;&nbsp;=20
&nbsp; B();<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;=20
~B()<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; friend void =
call(A=20
&amp;a, B &amp;b);<BR>};<BR><BR>void call(A &amp;a, B=20
&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--
.

User: "Frank-O"

Title: Re: how to declare a friend function that can access two class 23 Oct 2006 03:19:44 AM
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;
}
------=3D_NextPart_000_0045_01C6F6B8.F8416B10
Content-Type: text/html; charset=3Dgb2312
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 2408

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.2963" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>
<DIV class=3Daltbg2 id=3Dcode0
style=3D"CLEAR: both; BORDER-RIGHT: #698cc3 1px solid; PADDING-RIGHT: 10p=

x; 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=3D=CB=CE=CC=E5>how to declare a friend function that can access two =

class</FONT></DIV>

<DIV class=3Daltbg2
style=3D"CLEAR: both; BORDER-RIGHT: #698cc3 1px solid; PADDING-RIGHT: 10p=

x; 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=3D=CB=CE=CC=E5>it will look like the following</FONT></DIV>
<DIV class=3Daltbg2
style=3D"CLEAR: both; BORDER-RIGHT: #698cc3 1px solid; PADDING-RIGHT: 10p=

x; 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>
=20
------=3D_NextPart_000_0045_01C6F6B8.F8416B10--

.
User: "freegnu"

Title: Re: how to declare a friend function that can access two class 23 Oct 2006 04:28:36 AM
thanks for your reply
"Frank-O" <franck.nasse.oxinus@gmail.com>
??????:1161591584.280824.121840@f16g2000cwb.googlegroups.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--

.



  Page 1 of 1

1

 


Related Articles
Referencing C header file that has new as param?
Is that a constant expression?
is there anyway on dis blasted planet that knows an answer to this??
template function that accepts const and non-const
Rows: display a pattern that increments in each row
Class with functions that return STL containers with incomplete types
Is there any loop that can expressed in while that cannot be expressed in for..?
how to check that my programe respects the iso c++
Copy constructor for a class that contains a pointer to a base class type (newbie)
Creating a file that doesn't exist
Java's performance far better that optimized C++
What can pointers do that other things can't do?
Problem associating file types, that doesn't exist already, with my app
How can you declare function f that...
An int that is 4 bytes in native, and 12 bytes in .NET? Huh?
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER