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: Non-member operator overloading, linker complains - comp.lang.c++ ...... Post Question | Groups ... Problem is, that U don't specify body of friend ... const member functions in classes derived from templates ... cannot write std::map via ostream_iterator? - comp.lang.c++ ...... Post Question | Groups ... usr/include/g++/bits/stream_iterator.h: In member function ... const map_ss::value_type& value; friend ostream ... Changing access specifiers during inheritance - comp.lang.c++ ...... Post Question | Groups ... be called: It can be called by members and friends ... private public ever change ... overloaded member functions ... Bad use of stringstream temporary? - comp.lang.c++The basic question is whether the following line of ... Some of them are member functions (which can be called even ... eye with a sharp stick (like one of my old friends ... Const constructor - comp.lang.c++.moderated... You forgot to declare ArraySlice<T> to be a friend ... rules would be the same as for const/mutable member functions ... can work with both classes without raising questions. Singleton base class - comp.lang.c++.moderatedThe member of TestClass should also be initialize ... but you are not allowed to add either that function itself or a friend ... in C++ - Stack Overflow Recently I got question on ... template template specialization - comp.lang.c++.moderated ...... Post Question | Groups ... Problem is, that U don't specify body of friend ... const member functions in classes derived from templates ... How to cast using MSVC++ Intrinsics - comp.lang.asm.x86... Post Question | Groups ... and need to get a release version from a friend with ... calling member functions from WNPROC callback functions - comp ... Writing operator<< for std::vector - comp.lang.c++.moderated ...Some time later in a function or method ... { std ... std::endl; } To at least partially answer my question ... Non-member operator overloading, linker complains ... Gradebook template - comp.databases.filemakerPortals are your friend here-- if you are not familiar ... Vashon Island, WA (206) 463-1634 Associate Member ... it is a number field, you can use the Aggregate functions ... c++ - public friend swap member function - Stack OverflowIn the beautiful answer to the copy-and-swap-idiom there is a piece of code I need a bit of help: class dumb_array { public: // ... friend void swap(dumb_array ... In C++, what is the differences between a member function and a ...Best Answer: Use a member when you can, and a friend when you have to. Sometimes friends are syntactically better (e.g., in class Fred, friend functions ... 7/28/2012 12:55:22 PM
|