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

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.

Related

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--; }

Mixing overriding and overloading in C++ [duplicate]

This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 3 years ago.
I don't understand why I have this error error: no matching function for call to ‘Child::m(Mother&) when I try to compile my code.
From what I understand:
since the type of c is Child and Child::m have a parameter of type Child while m is of type Mother in c.m(m) then it's the function m() from the Mother class who need to be called.
class Mother{
public:
void m(const Mother&) {
}
};
class Child: public Mother{
public:
void m(const Child&) {
}
};
int main() {
Mother m;
Child c;
c.m(m);
}
Your Child class has no member function m that takes a Mother. It is hidden by the m declared in Child. You can unhide it by adding using Mother::m; in Child.

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

Protected Inheritance in C++ [duplicate]

This question already has answers here:
What is the difference between public, private, and protected inheritance in C++?
(16 answers)
Closed 6 years ago.
Simple code:
class A
{
private: int a;
protected: int b;
public: int c;
};
class B : protected A
{
};
class C : protected B
{
};
I know in Class B, a will remain private & b and c are protected.
But what I'm confused about is what will the access specifiers be in class C?
With protected inheritance inherited public members become protected.
With private inheritance inherited public and protected members become private.

Make a friend class have only special access to 1 function of another class? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is this key-oriented access-protection pattern a known idiom?
I have class A and class B. I want class A to access one of class B's private functions; but only that, not everything else. Is that possible?
Some kind of example:
class A {
//stuff
};
class B {
int r; // A cant use this
MagicFriendKeyword A void func(); // A can use this
public:
...
};
If there is one (or few) members functions in class A, that want to use class B's private member functions, then you can declare those one/few functions as friend. E.g.
class B {
// ...
friend void A::mutateB( B * );
// ...
};
See http://en.wikipedia.org/wiki/Friend_function