how to declare a friend function that can access two class

Discussion in 'C++' started by freegnu, Oct 23, 2006.

  1. freegnu

    freegnu Guest

    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;
    }
     
    freegnu, Oct 23, 2006
    #1
    1. Advertising

  2. freegnu

    Frank-O Guest

    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--
     
    Frank-O, Oct 23, 2006
    #2
    1. Advertising

  3. freegnu

    freegnu Guest

    thanks for your reply
    "Frank-O" <>
    ??????:...
    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--
     
    freegnu, Oct 23, 2006
    #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. Replies:
    1
    Views:
    1,399
    Jim Langston
    Nov 9, 2006
  2. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    710
    Victor Bazarov
    Aug 1, 2007
  3. Replies:
    4
    Views:
    449
    Victor Bazarov
    Sep 30, 2007
  4. Replies:
    2
    Views:
    695
    Triple-DES
    Feb 26, 2008
  5. Rui Maciel
    Replies:
    11
    Views:
    1,119
    Francesco S. Carta
    Jul 13, 2010
Loading...

Share This Page