C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Question about friend member functions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Gergely Korodi
Guest





PostPosted: Wed Jul 28, 2004 8:16 am    Post subject: Question about friend member functions Reply with quote




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! ]
Back to top
Alberto Barbati
Guest





PostPosted: Wed Jul 28, 2004 12:46 pm    Post subject: Re: Question about friend member functions Reply with quote



Gergely Korodi wrote:
Quote:
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! ]

Back to top
Victor Bazarov
Guest





PostPosted: Wed Jul 28, 2004 3:23 pm    Post subject: Re: Question about friend member functions Reply with quote



Gergely Korodi wrote:
Quote:
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! ]

Back to top
Michael D. Borghardt
Guest





PostPosted: Fri Jul 30, 2004 9:53 am    Post subject: Re: Question about friend member functions Reply with quote

Quote:
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! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.