First, why the introduction of friend mechanism? In general, to let non-member functions, ordinary functions or other classes can access the private members of the class, which does undermine the encapsulation of the class and data concealment, but
After adopting the class mechanism, the data hiding and encapsulation are realized, the data members of the class are generally defined as private members, and the member functions are generally defined as common, which provides the communication
A class can have members of public, protected, private three attributes that can access public members through objects, and only functions in this class can access private members of this class. Now, let's add an exception--friend.
Fnend means a
It is undoubtedly a great advantage of object-oriented programming to encapsulate data with functions of processing data, form classes and realize data hiding. But sometimes the encapsulation is not absolute.
A friend function provides a mechanism
Friend functions and friend classes are less used in actual development, and readers who want to learn C + + quickly can skip this section.A class can have members of public, protected, private three properties that can access public members, and
In some cases, it is more convenient to grant member-level access to functions that are not members of a class or to all functions in a separate class. Class-only implementations can declare their friends. A function or class cannot declare itself
First, the article for the reasonThe last one wrote a friend function, this time write a sister chapter, continue to delve into the friend class.Second, the definition
All member functions of a friend class are friend functions that also
Introduction of FriendsWe know that the member functions of a class can access other member functions of the same kind, including public, private, and protected members. The external function of a class can only access the public members of the
Friend functionThe private properties of a default class can only be accessed directly within the class.友元函数申明在内的内部,实现在类的外部可以直接访问类的私有属性。classa1{ Public: A1 () {a1 = -; A2 = $; }intGetA1 () {return This->a1; }//Declare a friend function friend
Recently these days in the re-design mode GOF23, accidentally think of friends, do not use all quickly forgetToday to revisit the C + + friend knowledge, after learning almost no how to use, almost all with the design pattern of dealing
Define member functions for the following two classes (in order to experience a friend class, in fact this example is not necessarily a good design, merging two classes into a datetime, the date and time are handled better)Class Date; Advance
1. The need for friend functions and friend classes:The class has encapsulation and information hiding attributes.Only member functions of a class can access private members of a class, and other functions in the program cannot access private
Friend functions can modify the private properties of a class, written under the public/private/protected of the class. When the function body of a friend function is written outside the class, it does not need to add the Friend keyword, just like a
The needs of friend functions and friend classes:Class has an attribute of encapsulation and information hiding. Only member functions of a class can access private members of a class, and other functions in the program cannot access private members.
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 classPermissions. If the base class is granted a friend relationship, only the base class
#include using namespace Std;Class MyClass1{Public:MyClass1 (): N (0) {};void Setn (int tmpn) {THIS->N=TMPN;}void Show () {CoutPrivateFriend classFriend class MyClass2;Use the friend function to change the value of the member variable n in the
#include using namespace Std;Class MyClass1{Public:MyClass1 (): N (0) {};void Setn (int tmpn) {THIS->N=TMPN;}void Show () {CoutPrivateFriend classFriend class MyClass2;Use the friend function to change the value of the member variable n in the
C + + friend class:Under normal circumstances, a class is not accessible to private members of another class. Just like this:#include class a{private: int a;public: A () { a = 1; }}; Class B:public A{public: B () {
/*All rights reserced.File name: main.cppLi XinCompletion Date: 2016.4.28Problem Description: Defines the member functions of the following two classes (in order to experience the friend class, this example is not necessarily a good design, merging
Define member functions for the following two classes (in order to experience a friend class, in fact this example is not necessarily a good design, merging two classes into a datetime, with a better date and time)./** Copyright (c) 2015, Yantai
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.