Question about friend member functions



 DEVELOP > c-Plus-Plus > Question about friend member functions

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Gergely Korodi"
Date: 28 Jul 2004 03:16:15 AM
Object: Question about friend member functions
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! ]
.

User: "Michael D. Borghardt"

Title: Re: Question about friend member functions 30 Jul 2004 04:53:14 AM

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! ]
.

User: "Victor Bazarov"

Title: Re: Question about friend member functions 28 Jul 2004 10:23:07 AM
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! ]
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER