C + + friend Friend Keyword Summary

Source: Internet
Author: User
Tags function definition modifier

Usually for ordinary functions, it is impossible to access the protection members of a class, and if you want to do so, you have to make the members of the class public (shared), but the problem with this is that any external function can access it without constraint, and C + + uses the friend modifier, You can allow some of the functions you set to operate on these protected data to avoid setting all class members to public and to protect data members to the maximum extent possible.

Friends can make common function direct access to class protection data, avoid the frequent invocation of class member functions, can save processor overhead, improve the efficiency of the program, but the paradox is that even the maximum protection, but also destroy the package characteristics of the class, which is the disadvantage of friends, Now the CPU speed is getting faster today we do not recommend it, but it as a necessary knowledge of C + +, a complete component, we still need to discuss.

1. Declare a normal function in a class, and then add a friend modifier to it, and the function becomes a friend of that class, and you can access all the members of that class.

Let's look at a piece of code to see how we use friends to access all the members of a class.

#include <iostream>
using namespace Std;
Class Internet
{
Public
Internet (char *name,char *address)
{
strcpy (Internet::name,name);
strcpy (internet::address,address);
}
friend void shown (Internet &obj);//friend function declaration
Public
Char name[20];
Char address[20];
};


void shown (Internet &obj)/function definition, cannot write, void Internet::shown (Internet &obj)
{
cout<<obj.name<<endl;
}
void Main ()
{
Internet A ("China Software Development Laboratory", "www.cndev-lab.com");
Shown (a);
Cin.get ();
}

The code above is defined by the friend function, we have successfully accessed the protection member name of object A, and the friend function is not considered a member function of a class, it is simply a normal function declared as a class friend, so the definition part of the class outer function cannot be written as a void Internet::shown ( Internet &obj), this should be noted.

  

2. A common function can be a friend function of multiple classes, we modify the code above to observe the changes:

#include <iostream>
using namespace Std;
Class Country;
Class Internet
{
Public
Internet (char *name,char *address)
{
strcpy (Internet::name,name);
strcpy (internet::address,address);
}
friend void shown (Internet &obj,country &AMP;CN);/note here
Public
Char name[20];
Char address[20];
};

Class Country
{
Public
Country ()
{
strcpy (CNAME, "China");
}
friend void shown (Internet &obj,country &AMP;CN);/note here
Protected
Char cname[30];
};

void shown (Internet &obj,country &AMP;CN)
{
cout<<cn.cname<< "|" <<obj.name<<endl;
}
void Main ()
{
Internet A ("China Software Development Laboratory", "www.cndev-lab.com");
Country b;
Shown (A,B);
Cin.get ();
}

A member function function of 31 classes can also be a friend of another class, so that the member functions of a class can manipulate the data members of another class, and we add a class of country to the following code, and watch:

#include <iostream>
using namespace Std;
Class Internet;

Class Country
{
Public
Country ()
{
strcpy (CNAME, "China");
}
void Editurl (Internet &temp);//member function declaration
Protected
Char cname[30];
};
Class Internet
{
Public
Internet (char *name,char *address)
{
strcpy (Internet::name,name);
strcpy (internet::address,address);
}
friend void Country::editurl (Internet &temp);//friend function declaration
Protected
Char name[20];
Char address[20];
};
Outer definition of void Country::editurl (Internet &temp)/member function
{
strcpy (temp.address, "edu.cndev-lab.com");
cout<<temp.name<< "|" <<temp.address<<endl;
}
void Main ()
{
Internet A ("China Software Development Laboratory", "www.cndev-lab.com");
Country b;
B.editurl (a);
Cin.get ();
}

4. The whole class can also be a friend of another class, which can also be called a friend. Each member function of a friend class can access all members of another class.
The sample code is as follows:

#include <iostream>
using namespace Std;
Class Internet;

Class Country
{
Public
Country ()
{
strcpy (CNAME, "China");
}
Friend Class internet;//'s statement
Protected
Char cname[30];
};
Class Internet
{
Public
Internet (char *name,char *address)
{
strcpy (Internet::name,name);
strcpy (internet::address,address);
}
void Editcname (Country &temp);
Protected
Char name[20];
Char address[20];
};
void Internet::editcname (Country &temp)
{
strcpy (Temp.cname, "People's Republic of China");
}
void Main ()
{
Internet A ("China Software Development Laboratory", "www.cndev-lab.com");
Country b;
A.editcname (b);
Cin.get ();
}

In the above code we successfully manipulated the protection member cname of the country class via the Internet Class Editcname member function.

Another important reason we use friends in programming is to facilitate the use of overloaded operators, which we'll focus on later in the tutorial.

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.