473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why can't member function be friend?

I want to show you an example how two classes can have relationship
instead of an inheritance. The inheritance is not an option if you
want to define two classes.
The relationship between two classes can be friends when variables
can be modified in both classes. As far as we discussed in an earlier
post, but I try to make this code much clearer.
The C++ Compiler generates an error if you try member function to be
friend because member function has not defined yet.
Only one member function can modify variable in another class. It
prevents you to add more member functions to modify variables in
another class. You may not want to use "friend class" as your best
option.
Do you have a way to fix an error?

Here is my example code here.

#ifndef CLASS_A_H
#define CLASS_A_H

class B;

class A
{
public:
A();
~A();
void Modify_A(B &b);
friend void B::Modify_B(A &); // error C2027: use of undefined type
'B'

private:
// friend class B;
int m_A1;
int m_A2;
};

#endif // CLASS_A_H
#ifndef CLASS_B_H
#define CLASS_B_H

class A;

class B
{
public:
B();
~B();
void Modify_B(A &a);
friend void A::Modify_A(B &); // error C2027: use of undefined type
'A'

private:
// friend class A;
int m_B1;
int m_B2;
};

#endif // CLASS_B_H
// CLASS_A.CPP
#include "Class_A.h"
#include "Class_B.h"

A::A() : m_A1(0), m_A2(0)
{
}

A::~A()
{
}

void A::Modify_A(B &b)
{
b.m_B1 += 3;
b.m_B2 += 4;
}
// CLASS_B.CPP
#include "Class_B.h"
#include "Class_A.h"

B::B() : m_B1(0), m_B2(0)
{
}

B::~B()
{
}

void B::Modify_B(A &a)
{
a.m_A1 += 1;
a.m_A2 += 2;
}
// MAIN.CPP

#include "Class_A.h"
#include "Class_B.h"

int main(void)
{
A a;
B b;

a.Modify_A(b);
b.Modify_B(a);

return 0;
}

Nephi
Sep 4 '08 #1
1 2504
Im************@hotmail.com wrote:
I want to show you an example how two classes can have relationship
instead of an inheritance. The inheritance is not an option if you
want to define two classes.
The relationship between two classes can be friends when variables
can be modified in both classes. As far as we discussed in an earlier
post, but I try to make this code much clearer.
The C++ Compiler generates an error if you try member function to be
friend because member function has not defined yet.
Yes. You sound surprised.
Only one member function can modify variable in another class. It
prevents you to add more member functions to modify variables in
another class. You may not want to use "friend class" as your best
option.
Do you have a way to fix an error?
You need a third class, a proxy, in which you will declare both A and B
as friends, and which you will define before either of those. Once it
is defined, you can make two functions in it to access private members
in A and in B, respectively, and make those members friends:

class A;
class B;
class AB_buddy {
static void access_As_privates_for_B(B& b, A& a);
static void access_Bs_privates_for_A(A& a, B& b);
};

class A {
friend AB_buddy::access_As_privates_for_B(B& b, A& a);
....
};

....
[...]
int main(void)
Use of 'void' to indicate no arguments is so C. Perhaps you can take up
the habit of putting *nothing* where *nothing* is expected:

int main()
{
A a;
B b;

a.Modify_A(b);
b.Modify_B(a);

return 0;
}

Nephi
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 4 '08 #2

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

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
12
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...
2
by: Gergely Korodi | last post by:
Hello, I have two classes, say A and B, defined in header files "A.hh" and "B.hh," respectively. The definition of class B looks like #ifndef _B_HH_ #define _B_HH_ #include "A.hh" //...
4
by: Justin Miller | last post by:
Ok, I tried to make that subject as descriptive as possible. What I'm trying to do: I'm attempting to use policies to create a generic memento (design pattern) template. My Memento template so...
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
6
by: ghager | last post by:
Hi all, I must be blind or stupid. Please consider the following code: ---- .... template <class T> class P; template <class T> P<T> operator*(T,const P<T>&); template <class T>
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
1
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...
7
by: PengYu.UT | last post by:
Hi, I want to write a test function to test a class. The class has a private member function that need to be tested. Although I could modify the member function as protected or public, I do not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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,...

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.