Combinatorial classes, friend functions, inner classes,

Source: Internet
Author: User

Before I mentioned the concept of combinatorial classes, I'll introduce two classes, friends, and inner classes today.

A combination class is an object in a class that has another class, such as declaring a class Penson, a member variable having a name, age, date of birth, and so on, declaring a date class, which is the information of the date of the month and year that is stored in the class. So we can set the date of birth of the person class to the date class, which makes up a composite class.

Friends of the concept, like a friend, such as Xiao Red said Xiao Ming is her friend, then for the small red, xiaoming must have other people do not have the privileges (why is the satin hand xiaoming Ah, Lao Wang?) 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0059.gif "alt=" j_0059.gif "/>" So the class is lifted into our class, which means A class can treat another class as a friend, giving him the right to access his own member variables (including private member variables, which is true to the heart, without a secret). Then another class, in its own class, can access the variables of that class just now.

Friends also have a concept of a friend function, similar to the friend class. In fact, each method inside the friend class is a friend function, the reader's own brain complement.

Friends of the characteristics, can not be passed (friend is not necessarily friends), can not Exchange (you treat him as a friend, he does not necessarily treat you as a friend, in this is he does not treat you as a friend), can not inherit (your father's friend is not necessarily your friend)

Friend class use Time:

Although a friend can access a member variable of a class, he breaks the encapsulation of the class and is therefore not recommended.

The overload of the operator is to use the friend class

When you want to share resources between classes and classes

The parameter Problem of friend function (friend function without this pointer) has 3 kinds of cases:

1, to access non-static members, the object needs to do parameters;

2, to access static members or global variables, the object does not need to do parameters;

3, if the object that does the parameter is global, then also does not need the object to do the parameter


inner classes, inner classes are also called nested classes. I personally like to call it an inner class, as the name implies, to define another class in the middle of a class. The rules for accessing the inner and outer classes are to have access to the public members of each other.

Before defining the inner class with public, (the class member default private, without the system default is to declare a private inner class), the inner class can not directly access the members of the external class, one way is through the external class object, one method is static access is: External class Name:: External class static member access

access method for members of the class (out -of-class:p ublic)

method One: the object name . member (variable or function)

pointer Variable à member (variable or method)

(* pointer variable). member (variable or method)

Method two: Class Name:: Member (variable or function)



This article is from the "Simon Eat Cattle" blog, please be sure to keep this source http://ximenchiniu.blog.51cto.com/9503623/1683383

Combinatorial classes, friend functions, inner classes,

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.