Question about friend member functions

G

Gergely Korodi

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
 
V

Victor Bazarov

Gergely said:
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
 
M

Michael D. Borghardt

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

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top