By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,507 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,507 IT Pros & Developers. It's quick & easy.

declaring class member functions as friend without full class definition

P: n/a
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,
Nov 11 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2008-11-11 16:19:07 -0500, "Hicham Mouline" <hi****@mouline.orgsaid:
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)

Nov 11 '08 #2

P: n/a
"Hicham Mouline" <hi****@mouline.orgkirjutas:
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

Nov 12 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.