"C + +" various member variables

Source: Internet
Author: User

From: Huang Bongyong Handsome

Const Constant Object:

The object is declared as a constant, that is, const hyong m, a const object cannot invoke a function that might alter the value of an object, so a const object can only invoke the constants constant function in the class , because a function that is not const can change the value of an object.

A const object can invoke a public member of a class, such as M.A, if it is public.

The public member of a const object cannot be re-assigned, as M.a=3 is wrong. However, you can reassign a public static member variable in a class because the static member is not part of the constant object, and he belongs to the entire class.

Object array:

An array of objects, each member of an array, is an object, such as Hyong A[3], where a[0],a[1],a[2] is an object of the Hyong type.

Initializes the object array, and if there is a default constructor the statement Hyong A[3] will invoke the default constructor to initialize 3 objects;

If the object array has a constructor with a parameter, you can initialize Hyong a[3]={1,2,3} like this.

If the object array has constructors with multiple parameters, the initialization method is Hyong A[3]={hyong, Hyong (3,4), Hyong (4,5)}.

Object members in the class:

That is, the object is a member of another class. such as class B{public:a X;}

If you want to initialize X with a constructor with parameters, you must initialize it with an initialization list .

Example:

#include <iostream>using namespacestd;classa{ Public:    intA, B; A () {a= B =0; cout<<"A Default constructor"<<Endl; } A (inti) {a= B =i; cout<<"A constructor for a parameter"<<Endl; } A (intIintj) {a=i; b=J; cout<<"a two constructor for a parameter"<<Endl; }};classb{ Public:    intA, B;    A x; B () {a= B =2; cout<<"B Default constructor"<<Endl; } B (inti) {a= B =i; cout<<"B constructor for one parameter"<<Endl; } B (intIintj);}; B::b (intIintJ): A (i), B (j), X (A (3,4)){}//Initializes a member with a constructor with parameters, with an initialization listintMain () {A m; cout<<"---------------"<<Endl;    B N; cout<<"---------------"<<Endl; B N1 (4,5); cout<<"---------------"<<Endl; N.x= A (6,7); cout<<"---------------"<<Endl; cout<< n.x.a << n.x.b <<Endl; return 0;}

If you do not have to initialize the list, in the constructor with two variables of the constructor to assign x value, the result is

#include <iostream>using namespacestd;classa{ Public:    intA, B; A () {a= B =0; cout<<"A Default constructor"<<Endl; } A (intIintj) {a=i; b=J; cout<<"a two constructor for a parameter"<<Endl; }};classb{ Public:    intA, B;    A x; B () {a= B =2; X= A (1,2);//This is no longer initialized.                     Instead, the value is re-assigned. //The x is generated with the default constructor of a, and a temporary variable is constructed with a, which assigns the temporary variable to x through the assignment function.cout <<"B Default constructor"<<Endl; }};intMain () {B n; return 0;}

Class member pointers

1. Declare the class member pointer in the following way: int hyong::* P1 declares a pointer to an integer member in the class P1. Int (hyong::* p2) () Note the parentheses, declaring a pointer to a parameterless function that has an inverse type of int P2

2. A pointer to a member of a class refers to pointers to members in the classes, rather than pointers to a member of the object, which is not the same as pointer p=&m.a.

   The class member pointer provides the offset of the member's object in the class, not a true pointer.

Because it is not a real pointer, you cannot access the members of the class through pointers , but only through special operators. * or->* to access the members pointed to by the pointer.

For example, *p1=2, Hyong::* p1=2 is wrong and cannot be assigned directly to a class member that the class member pointer points to. Cout<<*p1<

"C + +" various member variables

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.