Friend class and inheritance

Source: Internet
Author: User

C + + Primer has the following description: The friend relationship can not inherit. A friend of a base class has no special access to a member of a derived class
Permissions. If the base class is granted a friend relationship, only the base class has special access rights, and the derived class of the base class cannot access the class that is granted the friend relationship.

In practice, however, the VS compiler does not install the description described above, and the following rules contradict the above description but conform to the VS compiler's processing rules.

Note: The g++ compiler is needed to verify this.

1 inheritance problems with friend class

derived class C of friend B of Class A 1.1 cannot access private or protect member variables of Class A. But you can access a by using the interface provided by B. (nonsense definitely can)

#include <iostream> 
using namespace   std; 
Class B; 
Class A 
{ 
	int A; 
Public: 
	A (int x=0) {a=x} 
	Friend class B; 
Class B 
{ 
	int b; 
Public: 
	void Fun (a& ob) {cout << ob.a << Endl;} 
}; 

Class C:public B
{public 
: 
	//void fun2 (a& ob) {   cout <<ob.a <<endl;} The newly added function of a derived class cannot access a, and this sentence will be an error
}; 

void   Main () 
{ 
	a A (); 
	c C; 
	C.fun (a); C is a derived class of B that   can still be accessed through the function fun of base class B 


Friends of 1.2. base can access the Private,protect member variable of base through the derived class drived base, but cannot access drived private,protect member variables. (This seems to be a bit of a conflict with "C + + Primer")

Personal understanding: The DriveD object itself contains the Base,base friend Frnd can naturally access the base part.

#include <iostream> 
using namespace std; 
Class Base 
{ 
	int m_a; 
Public: 
	Base (int x=0) {m_a=x} 
	Friend class Frnd; 

Class Drived:public Base 
{ 
private:
	int m_c;
Public: 
	drived (int x): Base (x) {m_c=x;} 
};

Class Frnd 
{public
: 
	void Fun (base& ob) {cout <<ob.m_a << Endl;} 
	void Fun2 (drived& ob) 
	{
		cout << ob.m_a<<endl;
		cout <<ob.m_c<<endl; Compile error
	} 
}; 

int main () 
{ 
	drived D (1); 
	Frnd F; 
	F.fun (d); 
	F.fun2 (d);
	System ("pause");
	return 0;

3 Transfer problem of friend class

A's friend is the friend of B,b is C, that a friend is C. No, the friend class is not transitive.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.