Question about friend member functions

  • Follow


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"
// other includes here

class B {
      A a;
      int f (/*lots of parameters*/);
};

#endif

I'd like B::f to be a friend of class A.  Is it possible to do this?  If
I write

#ifndef _A_HH_
#define _A_HH_

#include "B.hh"

class A {
      friend int B::f (/*lots of parameters*/);
      // some other stuff
};

the compiler complains when it gets to the "A a;" line included from
B.hh, saying class A is not defined there yet.  Moreover, the parameters
of B::f are completely irrelevant to class A, so I would not like to
include the corresponding header files in A.hh just for them.  Is there
any solution to tackle this?

Thanks,

Gergo


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Gergely 7/28/2004 8:16:15 AM

Gergely Korodi wrote:
> 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"
> // other includes here
> 
> class B {
>       A a;
>       int f (/*lots of parameters*/);
> };
> 
> #endif
> 
> I'd like B::f to be a friend of class A.  Is it possible to do this?  If
> I write
> 
> #ifndef _A_HH_
> #define _A_HH_
> 
> #include "B.hh"
> 
> class A {
>       friend int B::f (/*lots of parameters*/);
>       // some other stuff
> };
> 
> the compiler complains when it gets to the "A a;" line included from
> B.hh, saying class A is not defined there yet.  Moreover, the parameters
> of B::f are completely irrelevant to class A, so I would not like to
> include the corresponding header files in A.hh just for them.  Is there
> any solution to tackle this?
> 

What about this:

#ifndef _A_HH_
#define _A_HH_

class B; // it's not necessary to include all "B.hh"

class A {
       friend class B;
       // some other stuff
};

The forward declaration is not strictly necessary, but makes the code 
more clear, IMHO.

Regards,

Alberto

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Alberto 7/28/2004 12:46:14 PM


Gergely Korodi wrote:
> 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"
> // other includes here
> 
> class B {
>       A a;
>       int f (/*lots of parameters*/);
> };
> 
> #endif
> 
> I'd like B::f to be a friend of class A.  Is it possible to do this?  If
> I write
> 
> #ifndef _A_HH_
> #define _A_HH_
> 
> #include "B.hh"
> 
> class A {
>       friend int B::f (/*lots of parameters*/);
>       // some other stuff
> };
> 
> the compiler complains when it gets to the "A a;" line included from
> B.hh, saying class A is not defined there yet.  Moreover, the parameters
> of B::f are completely irrelevant to class A, so I would not like to
> include the corresponding header files in A.hh just for them.  Is there
> any solution to tackle this?

I think, in general there is no direct solution because you're trying
to declare a member of a class without defining a class.  That would
be a "forward declaration of a member", which is not allowed.

You either have to declare the whole class B a friend or work around
having to declare a friend.  A global function could be declared outside
the class and then declared a friend of both classes, and you could just
call it in B::f if you need to.

Victor

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Victor 7/28/2004 3:23:07 PM

 > I'd like B::f to be a friend of class A.  Is it possible to do this?  If
 > I write
 >

If I understand what you are saying try using an intermediate helper
function that can be a forward declaration

a.h

#ifndef _A_HH_
#define _A_HH_

class B;
int helper(B *b);

class A {
public:
     A::A(B *b) : b_(b) {}
     void A::doSomethingToB() {int i = helper(b_);}
private:
     // some other stuff
     B* b_;
};
#endif

b.h

ifndef _B_HH_
#define _B_HH_

#include <iostream>
#include "a.h"

class B
{
public:
     B::B() : a(this) {}
     void doSomethingWithA() {a.doSomethingToB();}
private:
     friend int helper(B *b);
     A a;
     int f (/*lots of parameters*/) {std::cout << "B::f" << std::endl;return
0;}
};
#endif

main.cpp

#include "b.h"

int helper(B *b)
{
     return b->f();
}

int main()
{
     B b;
     b.doSomethingWithA();
     return 0;
}


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Michael 7/30/2004 9:53:14 AM

3 Replies
89 Views

(page loaded in 0.086 seconds)

Similiar Articles:













7/28/2012 12:55:22 PM


Reply: