Re: Declaring member function of another class as friend before it
is defined
On 6/27/2010 10:04 AM, Cotangent Alpha wrote:
When class A is declared before class B is defined, I am unable to
make B::baz() a friend function of class A. I get this error: member
'void B::baz()' declared as friend before type 'B' defined. Is it
possible to resolve this?
No. Your choices are either to make the entire class B A's friend or to
create a stand-alone function that would simulate the interface of
'B::baz' by taking 'B&' as its first argument and declare it A's friend
and probably B's friend as well (if you intend to access its private
members).
Here is the code:
class B;
class A
{
public:
void foo() {
cout<< "A::foo()"<< endl;
}
private:
// This function should be accessible to B only and nothing else.
void bar() {
cout<< "A::bar()"<< endl;
}
friend void B::baz();
};
class B
{
public:
void baz() {
A a;
a.bar();
}
};
int main()
{
B b = b;
b.baz();
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask