C + + Friend

Source: Internet
Author: User

1,friend Declare a friend

Friend is generally a statement that is located inside a class that declares a class or function as a friend of that class. Friend does not define a member function, so friend can be in public,protected or private before, completely the same. As a friend, it means that the private member of the class can be accessed within the class or inside the function, and there should be nothing hidden between you and your friends. Example:

Class A
{
Public
A (int _a): A (_a) {}
friend void Test (a&);
Friend class B;
Private
int A;
};

void Test (a& x)
{
x.a=100;//access rights for private member A
}

Class B
{
Public
void Foo ();
};

If a friend declaration is a generic non-template class or function, it can be declared for the first time. For a class, it can only be a declaration, and for a function, it could be its definition.

Class A
{
Public
A (int _a): A (_a) {}
friend void Test (a& x)
{
X.A = 100;//Definition:: Test () friend function
}
Friend class B;
Private
int A;
};

Note that although the definition of a function is placed inside a class, it is not a member function, and it becomes a global function for omitting the restricted definition form: Test (), and of course you can declare that the member function of another class is friend, such as:

Class A
{
Public
A (int _a): A (_a) {}
friend void B::foo ();
Private
int A;
};

In general, if you want to access a private member of Class A, write a statement about it in Class A and precede it with the Friend keyword.

This is the general situation, very simple, but it will destroy the original intention of the package, so as little as possible; effective C + + has an example of an application that defines a global function as a two-dollar operator for a class, if you want it to implicitly convert the operand, and declares it as a friend of that class.

2, template function as Friend

First, a template function, which is a template, is not a function:

Template<typename t>
void Foo1 (T);

When defining foo1 as a friend of a class, or to instantiate a template parameter T, or to give a deductive statement, and even if it can be deductive, a pair of angle brackets cannot be saved. Such as:

Class A
{
Public
friend void Foo1<char> (char);
friend void foo1<> (double);
};

Or give the restriction:::

Class A
{
Public
friend void:: Foo1 (char);
};

Of course, if there is a general function that has this form, it will take precedence over the template function match. Finally, the declaration of the formula can not be defined, must be pre-declaration (definition).

3, friends in the template class

The template class can also declare the friend in 2, but the template class has template parameters, if the use of the template parameters of the friend declaration, this is the case.

Template<typename t>
Class A
{
Public
friend void Foo1<t> (T);
};

However, here, it must be required that foo1 be visible here, that is, cannot be the first statement. If you don't use template parameters, that's an interesting situation.

Template<typename t>
Class A
{
Public
friend void Foo () {}
};

Note Here is a definition, which defines a: the Foo () function is a friend of the template class, and when the template class is present,: Foo () is also present, namely:

a<int> a1;//:: foo () First definition
a<char> a2;//:: foo () second definition, violating C + + one-time definition principle

4, Friend template

If you want to define a series of functions for the friend of the class, you can make the UF meta-template. It is similar to the template's declaration, except that it adds the Friend keyword after the template<>.

Class A
{
Public
Template<typename t>
friend void Foo ();
};

5, can be defined as the formula

The case that can be defined is: Unrestricted, no previous to::, no template argument list, no pair of angle brackets. If it is a template declaration, it cannot be the first declaration and must be visible in that place.

6, a complete example

Template<typename t>
Class Rat
{
Public
Rat (t _a, T _b): A (_a), B (_b) {}
Friend Rat<t> operator*<t> (rat<t>&,rat<t>&);
Private
T A, B;
};

Template<typename t>
Rat<t> operator* (rat<t> & x, rat<t> & y)
{
Return rat<t> (x.a*y.a,x.b*y.b);
}

Rat<t> is a rational number class of type T, defines its multiplication operation, defines a global function, and declares it to be a friend, and the function should also be a template, hopefully as the program is compiled. There is no declaration of operator* () before the friend type, so this cannot be the first declaration and must be preceded by a statement:

Template<typename t>
Rat<t> operator* (rat<t> & x, rat<t> & y);

Before that, there was no rat<t> statement, plus:

Template<typename t>
Class Rat;

By compiling, or changing to a friend Template:

Template<typename t>
Class Rat
{
Public
Rat (t _a, T _b): A (_a), B (_b) {}
Template<typename u>
Friend Rat<u> operator* (rat<u>&,rat<u>&);
Private
T A, B;
};

Template<typename t>
Rat<t> operator* (rat<t> & x, rat<t> & y)
{
Return rat<t> (x.a*y.a,x.b*y.b);
}

There are a small number of different,rat<t> affirmed a series of friends Operator*<u>, of course, not necessary, as long as operator*<t> is enough, but the form is simple. There is also a simpler form of putting a definition in it, which is the method used in effective C + +:

Template<typename t>
Class Rat
{
Public
Rat (t _a, T _b): A (_a), B (_b) {}
Friend Rat<t> operator* (rat<t>&x,rat<t>&y)//Definition and Affirmation:: operator* ()
{
Return rat<t> (x.a*y.a,x.b*y.b);
}
Private
T A, B;
};

C + + Friend

Related Article

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.