Are friends in c++ mutual? [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Friend scope in C++
Are friends in C++ mutual?

class bar
{
private:
void barMe();
};
class foo
{
private:
void fooMe();
friend bar;
};
In the above example foo class can't call barMe()
You need to define the classes this way in order that the friend be mutual:
class foo; // forward
class bar
{
private:
void barMe();
friend foo;
};
class foo
{
private:
void fooMe();
friend bar;
};

The friend relationship is only one-way in general - but there is nothing to stop you declaring Class A a friend of class B AND class B a friend of class A. So a mutual relationship can be established

Related

Classes With Friend Functions of Each Other

Is it possible to create a class that has a friend function of a forward declared class? If I wanted to create two classes that have friend functions of each other, how would I do that? This attempt fails with the message invalid use of incomplete type 'class Y':
class Y;
class X
{
public:
void a();
friend void Y::b();
};
class Y
{
public:
void b();
friend void X::a();
};
I also tried forward declaring void Y::b(); right after class Y; which similarly did not work. Would this example be an indicator of bad design on my part or just a limitation of C++?

dependent classes friend functions, are they possible?

I was reading an old C++ book to do some retro coding, C++ for C Programmers by Sharam Hekmatpour, I remember reading it when I was way younger and I liked it.
In the friend functions chapter he shows having two classes with friend functions:
class RealSet;
class IntSet {
public:
void ToRealSet(RealSet*);
friend void RealSet::ToIntSet(IntSet*);
};
class RealSet {
public:
void ToIntSet(IntSet*);
friend void IntSet::ToRealSet(RealSet*);
};
I know this won't compile because while we had defined RealSet as a forward declaration, we don't have a definition to RealSet::ToIntSet and that will make the compiler fail.
Yes, I know I could set the whole class as a friend class and that will solve the problem:
class RealSet;
class IntSet {
public:
ToRealSet(RealSet*);
friend class RealSet;
};
class RealSet {
public:
ToIntSet(IntSet*);
friend class IntSet;
};
My question is simple: Was this ever possible or is it a typical book mistake?

Friend function - member is inaccessible [duplicate]

This question already has answers here:
Specify a class member function as a friend of another class?
(8 answers)
Closed 3 years ago.
I'm taking a C++ course, and I came across something I can't wrap my head around. I've tried searching for an answer, but I've come up short.
class A {
friend void C::dec(A&);
private:
int field;
};
class C {
public:
void dec(A& a);
};
void C::dec(A& a) { a.field--; } <-- member A::field is inaccessible
I am aware that for this to work, class A should be declared before, but defined after, class C. But I'm struggling to understand why.
So why is the class member A::field inaccessible when class A is defined before class C?
The problem is not the friend declaration, it's just C is unknown where you declare it in A.
So you define C and forward declare A, then just define C as you already did.
class A;
class C {
public:
void dec(A& a);
};
class A {
friend void C::dec(A&);
private:
int field;
};
void C::dec(A& a) { a.field--; }

Sometimes class members are not inherited by subclasses [duplicate]

This question already has answers here:
Why do I have to access template base class members through the this pointer?
(3 answers)
Derived template-class access to base-class member-data
(3 answers)
Closed 5 years ago.
When we create a template class example1 which derives from another template class example2. The template class example1 does not inherit systematically of example2 members.
Consider the code bellow:
template<class T>
class example2 {
protected:
int a;
public:
int getA() const {return a;}
};
template<class T>
class example1 : public example2<T> {
T* p;
public:
void test() {
example2<T>::a = 8;
}
};
in the method test of the class example1, we have to use example2<T>:: operator in order to be able to access to the member a. Otherwise, the complier generates an error: 'a' was not declared in this scope.
I mean, why the complier does not recognize a in the class example1? Why we should specify example2<T>::a and not just a?
Thanks

How does "friendship" work for derived classes? [duplicate]

This question already has answers here:
Why does C++ not allow inherited friendship?
(9 answers)
Closed 8 years ago.
There are three classes:
class A
{
friend I_B;
protected:
void* mData;
};
class I_B
{
void foo() = 0;
};
class B_Impl : public I_B
{
B_Impl( A* value )
:
mData( value->mData ) <--- ERROR
{
}
void foo() { mData->DoSomething() };
protected:
void* mData;
};
At compile time I get an error in the constructor, that mData is a protected member.
Please explain me please why it happens.
Can I get access to protected members using "friendship" of the base class?
Friendship is not inherited. If you want B_Impl to be a friend of A you must declare B_Impl as a friend.
Friendship is also not transitive: your friend's friend is not necessarily your friend.