[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

declaring class member functions as friend without full class definition

Hicham Mouline

11/11/2008 9:19:00 PM

Hello,

I am attempting to change a existing large code base and I have this
situation:

A.hpp
class A {
....
friend void B::member_function1() const;
...
};


When I include B.hpp for the definition of B, I seem to fall in
cross-include situation where the include guards
don't work (or work bad...)

I tried forward declaring class B before A, but it seems the full definition
of B is required
to declare its member function as a friend to A.

Is there a solution to this?

regards,


2 Answers

Pete Becker

11/11/2008 10:22:00 PM

0

On 2008-11-11 16:19:07 -0500, "Hicham Mouline" <hicham@mouline.org> said:

> Hello,
>
> I am attempting to change a existing large code base and I have this
> situation:
>
> A.hpp
> class A {
> ....
> friend void B::member_function1() const;
> ...
> };
>
>
> When I include B.hpp for the definition of B, I seem to fall in
> cross-include situation where the include guards
> don't work (or work bad...)
>
> I tried forward declaring class B before A, but it seems the full definition
> of B is required
> to declare its member function as a friend to A.
>
> Is there a solution to this?
>

Sure. Put the full definition of B before the definition of A. Since
you haven't shown any other code, it's impossible to guess the causes
of whatever other problems you've been running into.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Paavo Helde

11/12/2008 7:44:00 AM

0

"Hicham Mouline" <hicham@mouline.org> kirjutas:

> Hello,
>
> I am attempting to change a existing large code base and I have this
> situation:
>
> A.hpp
> class A {
> ....
> friend void B::member_function1() const;
> ...
> };
>
>
> When I include B.hpp for the definition of B, I seem to fall in
> cross-include situation where the include guards
> don't work (or work bad...)
>
> I tried forward declaring class B before A, but it seems the full
> definition of B is required
> to declare its member function as a friend to A.

Correct.

>
> Is there a solution to this?

If it is not possible to move B definition before A, then the simplest
solution is to define the whole (forward-declared) class B as a friend.
Another solution would be to remove the friend declaration and instead
define a member function in A exposing or twiddling the private bits, and
document that only B::member_function1() can call it.

The cleanest solution would be to redesign the code so that friendship is
not required. However, this may require enormous effort in case of an
"existing large code base".

Paavo