Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why can't member function be friend?

0 views
Skip to first unread message

Immorta...@hotmail.com

unread,
Sep 4, 2008, 2:01:33 PM9/4/08
to
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

Victor Bazarov

unread,
Sep 4, 2008, 2:10:29 PM9/4/08
to
Immorta...@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

0 new messages