C + + Knowledge point 2

Source: Internet
Author: User

I haven't written an article in a long time.first send the notes of last month:)Basic Type Constantsconst int A; int const A; const int *a; int * const A; int const * a const;
const int A; int const A; The two formulations are identical, indicating that a is an int constant.
const int *a; Indicates that a is a pointer that can point to an int constant or an int variable, and always treats the target it points to as an int constant. Can also be written as int const* A;
int * const A; Indicates that a is a pointer constant that must be fixed to an int variable when initialized, and then no longer point to another place.
int const * A const; it can be written as int const * const A; A is a pointer constant, which must be fixed to an int constant or int variable when initialized, and then no longer point to another place. It always treats the target it points to as an int constant. It can also be written as Const int* const A;

The notation of the int const a=1 and the const int a=1 in C/s + + is equivalent

Constant object

A constant type is a type that uses the const description of a type modifier, and the value of a variable or object of a constant type cannot be updated. Therefore, you must initialize when you define or describe a constant type.

General constants and Object constants

1. General constants

A general constant is a constant of a simple type. When this constant is defined, the modifier const can be used in front of the type descriptor, or it can be used in the type description specifier. Such as:

int const x=2;

Or

const int x=2;

Define or describe a constant group that can be in the following format:

< type descriptor > Const < array name >[< size;] ...

Or

Const < Type descriptor > < array name >[< size;] ...

For example:

int const A[5]={1, 2, 3, 4, 5};

2. Constant objects

Constant objects are object constants, which are defined in the following format:

< class name > Const < object name >

Or

Const < class name > < object name >

When a constant object is defined, it is also initialized and the object can no longer be updated, and the modifier const can be placed after the class name or placed before the class name.

Constant pointers and common references

1. Constant pointer

When you use the const modifier pointer, the meaning differs because of the position of the Const. Here are two examples to illustrate their differences.

A constant pointer to a string, as defined below:

char * Const PRT1 = Stringprt1;

Where PTR1 is a constant pointer. Therefore, it is illegal to assign the following values.

PTR1 = Stringprt2;

And the following assignment is legal:

*PTR1 = "M";

Because the variable pointed to by pointer Ptr1 is updatable, non-updatable is the direction (other string) that the constant pointer ptr1 refers to.

The following defines a pointer to a string constant:

Const * PTR2 = STRINGPRT1;

Where PTR2 is a pointer to a string constant. The string that ptr2 points to cannot be updated, and PTR2 is updatable. So

*PTR2 = "X";

is illegal, and:

PTR2 = STRINGPTR2;

is legal.

Therefore, when you use the const modifier pointer, you should pay attention to the position of the Const. When defining a pointer constant to a string and defining a pointer to a string constant, the const modifier is placed in a different position, the former const between the * and the pointer name, and the latter const placed in the type description match either.

2. Frequently cited

You can also use the Const modifier to describe a reference, the referenced reference is a constant reference, and the object referenced by the reference cannot be updated. The format is defined as follows:

Const < Type descriptor > & < reference name >

For example:

Const Double & V;

In practical applications, constant pointers and constant references are often used as formal parameters for functions, and such parameters are called constant arguments.

In C + + object-oriented programming, pointers and references are used more frequently, where const-decorated constant pointers and regular references are used more often. Using a constant parameter indicates that the function does not update the object that a parameter points to or refers to, so that the copy initialization constructor is not required during parameter passing, which will improve the efficiency of the program's operation.

The following example illustrates the practice of constant pointers as function parameters.

#include
const int N = 6;
void print (const int *p, int n);

void Main ()
{
int array[n];
for (int i=0; i<n; i++) <br= "" > cin>>array[i];
Print (array, N);
}

void print (const int *p, int n)
{
cout<< "{" <<*p;
for (int i=1; i<n; i++) <br= "" > cout<< "," <<* (P+i);
cout<< "}" <<endl;
}


  Constant member function

A member function, described by using the Const keyword, is called a constant member function. Only constant member functions are eligible to manipulate constants or constant objects, and member functions that are not described with the Const keyword cannot be used to manipulate a constant object. The regular member function description format is as follows:

< type descriptor > < function name > (< parameter table >) const;

Where const is the type modifier appended to the function description, which is an integral part of the function type, so the const keyword is also included in the function implementation section. The following example illustrates the characteristics of a constant member function.

#include
Class R
{
Public
R (int r1, int r2) {r1=r1; R2=R2; }
void print ();
void print () const;
Private
int R1, R2;
};

void R::p rint ()
{
cout<<r1<< "," <<r2<<endl;
}

void R::p rint () const
{
cout<<r1<< ";" <<r2<<endl;
}

void Main ()
{
R A (5, 4);
A.print ();
Const R B (20, 52);
B.print ();
}


The output of this example is:

5,4
20;52

The class of the program declares two member functions whose types are different (in fact, overloaded member functions). The const constants are handled by member functions with the Const modifier, which also shows the features of function overloading.

Regular data members

The type modifier const can describe not only member functions, but also data members.

Because the Const type object must be initialized and cannot be updated, when a const data member is described in a class, the constructor can only be generated by the member initialization list to initialize the data member.

The following is an example of using a member initialization list to generate a constructor function.

#include
Class A
{
Public
A (int i);
void print ();
const INT &r;
Private
const int A;
static const int B;
};

const int a::b=10;
a::a (int i): A (i), R (a)
{
}

void A::p rint ()
{
cout<<a<< ":" <<b<< ":" <<r<<endl;
}

void Main ()
{
A A1 (+), A2 (0);
A1.print ();
A2.print ();
}


The result of this program is:

100:10:100
0:10:0

In this program, the following three constant type data members are described:

const INT & R;

const int A;

static const int B;

where r is a constant int reference, A is a constant int variable and b is a static constant int variable.

Static data member B is initialized in the program.

It is important to note that the format of the constructor is as follows:

A (int i): A (i), R (a)
{
}

Where the colon is followed by a data member initialization list, which contains two initializers, separated by commas, because data members A and r are common types and need to be in an initialization format.

As with normal functions, the value of a parameter in a constructor can be passed either through an argument or as a default value, that is, if the user does not specify an argument value, the compilation system makes the formal parameter the default value. The constructor can also use the initialization list,

C + + Knowledge point 2

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.