Friend function, overloaded operator and array __ function in class

Source: Internet
Author: User
Tags date1 function definition
1.
bool Equal (DayOfYear date1,dayofyear date2)
{return
   (date1.month==date2.month&&date1.day== date2.day);
}
There is only one problem with this definition: it is illegal. This is illegal because the member variable month and day are private members of the DayOfYear. Private member variables (and private member functions) are usually not referenced in the body of a function, unless the function is a member function.
A friend function of a class is not a member function of this class, but rather a "friendly" function that accesses the private members of that class like a member function. A friend function can directly read the value of a member variable, or even directly change the value of a member variable.
When a declaration of a function is listed in the definition of a class, the. This function becomes a friend of the class, and you simply add a keyword friend before the function declaration. A friend is not a member function, it is essentially a normal function, but is specifically granted access to the data members of the class.
2. A simple rule can help you select member functions and non-member functions
1. If the function is to perform a task that involves an object, the member function is used.
2. If you want to perform a task involving multiple objects, use a non-member function.
3.const parameter modifier
Const applies to the calling object:
In the case of a member function, the keyword const is placed after the function declaration, just before the semicolon at the end, as follows:
Classs Money
{public
:
    ...
    void output (ostream &outs) const;
Const is used in function declarations and function definitions, so the output function definition begins as follows:
void Money::output (Ostream & const)
{
    ...
}

4. Overloaded operator

Rules:

1. When overloading an operator, at least one argument must be a class type.

2, the overloaded operator can be a friend of the class; an operator function can be a member of a class, or it can be a normal function.

3, you cannot create a new operator. You can only overload existing operators, such as + 、-、 *,/,%, and so on.

4, can not change the priority of an operator.

5, the following operators can not be overloaded: dot operator, scope resolution operator:: and Operators. * and?:


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.