If I have two Classes as follows with inheritance:
class A
{
...
}
class B : public A
{
...
}
And a third class with defined as a friend class A:
class C
{
friend class A;
}
Will I be able to access from class B
(which is also an object of type A
) all members of class C
as if I had defined class B
the friend Class in the first place?
friend
ship is neither inherited nor transitive. It is strictly one-one relationship between two classes.
class A {
friend class B;
int Aries;
};
class B {
friend class C;
int Taurus;
};
class C {
int Leo;
void Capricorn() {
A a;
a.Aries = 0; // this wont work, C is not a friend of A.
// friendship is not transitive
}
};
class D : public C {
void Gemini() {
B b;
b.Taurus = 0; // this wont work, D is not a friend of B.
// friendship is not inherited
}
};
class E : public B {
void Scorpio() {
C c;
c.Leo = 0; // this wont work either, friendship is not inherited
}
};
Reference: "The C++ Programming Language" Bjarne Stroustrup
More explanation (mine): If friend
ship were not one-one, it would be the end of encapsulation. Note that B
class can access private
members of A
only if the class declaration of A
declares B
as friend
. B
cannot enforce friend
ship on A
.
Now, if friendship could be inherited, then someone just needs to inherit B
to access private members of A
, without A
having any say in preventing it. Also, allowing friend
ship to be transitive would lead to other problems, since now B
could have a friend
C
, who in turn could have a friend
D
, all the way to Z
. All of B
, C
, D
, ..., Z
can now access A
's private
members, which would be a disaster.
For some reason everybody has forgotten that you can access virtual private functions of a class that derives from the friendship-giver.
#include <iostream>
class Friend;
class Parent
{
friend class Friend;
private:
virtual void nameYourself() { std::cout << "Parent" << std::endl; }
};
class Child : public Parent
{
private:
virtual void nameYourself() { std::cout << "Child" << std::endl; }
};
class Friend
{
public:
void foo(Parent *p) { p->nameYourself(); }
};
int main()
{
Parent p;
Child c;
Friend f;
f.foo(&p);
f.foo(&c);
return 0;
}
The output of running above code is:
Parent
Child
The reason why it works is tricky and has to do with how this pointer is passed (look up vtables). Were you to remove "virtual" keyword from the function declaration and you will lose this ability.
To quote the standard, C++11 11.3/10:
Friendship is neither inherited nor transitive.
Meaning that neither derived classes of friends nor friends of friends receive the benefits of friendship.
One more example adding to Masked Man's answer/code above that I found useful:
class B {
friend class F;
friend class E;
int Taurus;
};
class E : public B {
int Egg;
};
class F {
void Foo () {
B b;
b.Taurus = 4; //Works F is friend of B (of course)
E e;
e.Taurus = 6; // Works E is derived from B and F is friend of B
// Taurus is private member of B.
e.Egg = 5; // Does not work, F is friend of Base Class B but not
// E, so can't access members of the derived class
}
};
I think it depends. You will have access from the A part of B (the sliced part). If you’ve defined B’s own function, I think you won’t.
I didn't understand what you are trying to do, but B is a superclass of A. Is not an object. Between B and C there isn't any inheritance relationship
No, you can't call C
class methods directly, but you can access them by pointer. A
class should be modified:
class C
{
void method()
{
}
friend class A;
};
class A
{
protected:
constexpr static void (C::*f)() = &C::method;
};
class B : public A
{
B()
{
//C().method(); ← This wont work
(C().*f)(); // ← This works fine
}
};
Accessing data members and static data are simple too