473,890 Members | 1,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to declare a friend function that can access two class

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;
}
Oct 23 '06 #1
2 2056
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.F 8416B10
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;&nb sp;int i;<BR>&nbsp;&nb sp;public:<BR>& nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nb sp;A(){}<BR>&nb sp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nb sp;~A(){}<BR>&n bsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; friend void call(A &amp;a, B
&amp;b);<BR><BR >};<BR><BR>clas s A<BR>{<BR>&nbsp ;&nbsp;private: <BR>&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nb sp;int
j;<BR>&nbsp;&nb sp;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;co ut &lt;&lt; a.i &lt;&lt; endl &lt;&lt; b.j
&lt;&lt;endl;<B R>}</DIV></FONT></DIV></BODY></HTML>

------=_NextPart_000_ 0045_01C6F6B8.F 8416B10--
Oct 23 '06 #2
thanks for your reply
"Frank-O" <fr************ *****@gmail.com >
??????:11****** *************** *@f16g2000cwb.g ooglegroups.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.F 8416B10
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;&nb sp;int i;<BR>&nbsp;&nb sp;public:<BR>& nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nb sp;A(){}<BR>&nb sp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nb sp;~A(){}<BR>&n bsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; friend void call(A &amp;a, B
&amp;b);<BR><BR >};<BR><BR>clas s A<BR>{<BR>&nbsp ;&nbsp;private: <BR>&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nb sp;int
j;<BR>&nbsp;&nb sp;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;co ut &lt;&lt; a.i &lt;&lt; endl &lt;&lt; b.j
&lt;&lt;endl;<B R>}</DIV></FONT></DIV></BODY></HTML>

------=_NextPart_000_ 0045_01C6F6B8.F 8416B10--

Oct 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3261
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always private. I have placed "friend void CA::Run_A(void)" in CMain Class. CMain::Run() function attempts to execute CA::Run_A(), but compiler shows an error saying that it is the violation to access private function. I don't understand why because friend...
2
1552
by: Kai Wu | last post by:
Hello, struct S{ friend class B; private: int i; }; class B{ B(){}
15
6607
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
5
2144
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
1
3309
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the dog's members can access all the cat's private members. // cat.h //
7
2006
by: =?gb2312?B?wfXquw==?= | last post by:
Hi folks, I am running into with such a question when I tried to declare and define a friend template function in a template class, here is the code snippet: #include <iostream> using namespace std; template<typename T> class Test {
3
3892
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: #include <iostream>
19
2731
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use public data members. Don't use friends, except to avoid or . Consider the following scenarios: Scenario 1:
2
2435
by: Immortal Nephi | last post by:
I design a class for general purpose. I do not allow a client to read or modify interface and implemention. I allow them to write a new non- member function outside of class' interface and implmention. The problem is that non-member function cannot access private data member. The friend keyword is not the solution. I am aware that friend keyword allows the non-member function to access private data member. If you define more than...
0
9978
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weĺll explore What is ONU, What Is Router, ONU & Routerĺs main usage, and What is the difference between ONU and Router. Letĺs take a closer look ! Part I. Meaning of...
0
11227
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10812
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10922
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9630
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectŚplanning, coding, testing, and deploymentŚwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8016
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph DuprÚ who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.