Simple analysis of C + + friend

Source: Internet
Author: User

C + + friend, including friend function and friend//c++ friend function//c++ Friend class/* When a Class A declares another class B as its friend class, you can access the private content of a in B */#include <iostream>        Using namespace Std;class a{Private:friend class B;        Friend class C;        friend void Friend_func (A & A);        friend void Friend_func_pointer (A *a);    int i;        PUBLIC:A (int II): I (ii) {cout<< "constructor A" <<endl;        } ~a () {cout<< "Deconstructor A" <<endl;        } void Func () {cout<< "A.I = =" <<this->i<<endl;            } void set (int ii) {this->i = II;        cout<< "Set a.i =" <<this->i<<endl;        }};class b{public:b () {cout<< "constructor B" <<endl;        } ~b () {cout<< "Deconstructor B" <<endl; } void Func (a& a) {cout<< "friend of A is B, something isDone in B, A.I = "<<a.i<<endl;        Return }};//here we're going to do some evil things, what if Class C, even if A's derived class, is a friend class?//let ' s has a try//we know that even if C is a derived class, the private member of its base class cannot be accessed in C/    So what would be interesting if a class was both its derived class and its friend class? class C:public a{Private:int J;        PUBLIC:C (int II, int JJ): A (ii), J (JJ) {cout<< "constructor C" <<endl;        } ~c () {cout<< "Deconstructor C" <<endl; } void Func (a& a) {cout<< "Do something with out telling A. A.I" <<a.i<<endl        ;    }};//we define a friend function void Friend_func (a& a) {cout<< "I am A friend Func of a, A.I =" <<a.i<<endl; return;} void Friend_func_pointer (a *a) {cout<< "I am a friend func of a, which receives of pointer of a, A.I =" <<a- >i<<endl;}    int main (int argc, char** argv) {b b;    A (1); cout<< "sizeof (b) =" <<sizeof (b) <<endl;//here the size of the output B is 1Byte, isn't it strange? The Ghost knows what C + + wants to do cout<< "sizeof (a) =" <<sizeof (a) <<endl;//here the output is still 4 bytes, that is, the size of an int,    Stating that a friend is declared in a does not cause a's volume to become larger cout<< "SIZOEF (c) =" <<sizeof (c) <<endl;    B.func (a);    C C (for all);    C.func (a); C.func ();//The name hiding, which we talked about earlier, causes the compilation to fail Friend_func (a);//We declare in a that Friend_func is a friend of a function, Then this function can access the private members of A//Let's do something exciting, what happens when a friend encounters Up_cast?    Let's give it a try = =! Friend_func (c);//No problem at all, we have access to something in C.    In fact, conceptually, we still have access to something in a.//What happens if you encounter pointers to Up_cast? Try and know + + friend_func_pointer (&a); Friend_func_pointer ((A *) &c);//There is no problem at all. return 0;} /* There are some concepts to note about Friends (1) A friend relationship cannot be inherited.    B is the friend of a class,c is the subclass of a, but B is not a friend of C. This is also very well understood. From the perspective of encapsulation, friend Class B can access the private content of a, but should not see the private object of its subclass. Not even a can see, let alone its friend Class B. (2) Friend relationship is one-way and not commutative.    If Class B is a friend of Class A, class A is not necessarily a friend of Class B, it depends on whether there is a corresponding declaration in the class. This better understand, a said: B, you are my friend, all my things can show you, but B said: Well, you wishful thinking, I have no way. But I still can't show you anything.    (3) Friend relationship does not have transitive nature.    This is also better understood, if B is a friend class, a is a friend of D.    Then B can see the private,a of a can see the private of D, but B should not directly see the private of D. SimpleThe example is that my friend's friend doesn't necessarily know me. *///A friend function, a function that defines the outside of a class, but can directly access the private member of the class//We'll come up with an example of the above code. */Summary: 1. A word, disorderly 2. Personal opinion Friend class and friend Function destroys the encapsulation feature of the OOP language. 3. As you can see from the above example, when friend function encounters a up_cast, friend function destroys the contents of the derived class.  4.any, I think it is still not a last resort, do not use friend class and friend function bar. After all, it is an uncivilized phenomenon to move others ' underwear without others ' perception. */

  

Simple analysis of C + + friend

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.