C + + friend learning notes __c++

Source: Internet
Author: User

Friends can be divided into three kinds:

1. Friend function;

2. Friend class;

3. Friend member function;

The benefit of friends, by using the friend function, you can give the function the same access as the class member function, which is a non-class member function that can access the private members of the class.

Because a friend function is not a member function of a class, it is not possible to invoke a member function in the same way as a class object (class member).

Creation of friend functions

Template<class t> class Matrix {friend ostream& operator<< (ostream& os, const matrix<t>& m {for (int i = 0; i < m.therows. i++) {for (int j = 0; J < M.thecolumns; J +) {OS << M.E
			Lement[i*m.thecolumns + j]<< "";
		Os << Endl;
		Os << "Output Matrix finished" << Endl;
		
	return OS;
	}; Friend istream& operator >> (istream& os, matrix<t>& m) {size_t number = M.thecolumns * M.ther
		oWS
		for (int i = 0; i < number; i++) {OS >> m.element[i];
		cout << "Input fanished" << Endl;
	return OS;
};
	Public:matrix (int therows = 0, int thecolumns = 0);
	Matrix (const matrix<t>&);
	~matrix () {delete[] element;};
	int rows () const {return therows;};
	int cols () const {return thecolumns;};
	t& operator () (int i, int j) const;
	matrix<t>& operator= (const matrix<t>&);
	Matrix<t> operator+ () const; MatriX<t> operator+ (const matrix<t>&) const;
	Matrix<t> operator-() const;
	Matrix<t> operator-(const matrix<t>&) const;
	Matrix<t> operator* (const matrix<t>&) const;
	matrix<t>& operator+= (const t&);
	void Input ();
	void Output ();
void Transport ();
	Private:int therows;
	int thecolumns;
t* element; };
Friend ostream& operator<< (ostream& os, const matrix<t>& m);
Is the way to create a friend function;

function is created, you need to define the

The definition of a friend function requires neither the class header nor the keyword friend;

<< is the most common friend function, overload << operator

In peacetime programming we often use cout, this is a Ostream object, it is intelligent, can recognize the basic types of C + +.

As a friend function overload << is to achieve the output of class object to bring convenience;

Today I fell into a hole in the friend, in theory, it is okay to define a friend function outside of the class, but the way I define it is that the program compiles unsuccessfully and always reports illegal use of explicit templates

Template<class t>
std::ostream& operator<< <T> (ostream& os, const matrix<t>& m)
{for

	(int i = 0; i < m.therows. i++)
	{for
		(int j = 0; J < M.thecolumns; J +)
		{
			OS <& Lt M.element[i*m.thecolumns + j] << "";
		}
		Os << Endl;
	}
	Os << "Output Matrix finished" << Endl;
	return OS;
Looking for a long time on the internet did not solve the problem, in the groping, I directly define the friend function within the class, and then the smooth running of the program, as for the truth, I still need to grope for groping.

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.