Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are derived classes considered friends?

If I create base class A and A is a friend of class B, can a class derived from A access B to its liking, or else what is it allowed?

Thanks

like image 428
jmasterx Avatar asked Nov 03 '10 02:11

jmasterx


People also ask

Can a derived class be a friend of the base class?

It is not possible.

What does a derived class mean?

What is a Derived Class? A derived class is a class that is constructed from a base class or an existing class. It has a tendency to acquire all the methods and properties of a base class. It is also known as a subclass or child class.

Does derived class inherit friend function?

Difference between Inheritance and Friendship in C++: In C++, friendship is not inherited. If a base class has a friend function, then the function doesn't become a friend of the derived class(es).

Do derivation and friendship mean the same in C++?

Both concepts are completely different.


1 Answers

struct A{};

struct Ader : A{};

struct B{
   friend struct A;
};

No. Friendship is not inherited in C++. It is also not transitive.

Ader can not access B as a friend unless explicitly given friendship by B, just because it's base A is a friend of B.

like image 164
Chubsdad Avatar answered Sep 20 '22 08:09

Chubsdad