"C + + Learning notes" Const usage __c++

Source: Internet
Author: User
Tags modifier

C + + const allows you to specify a semantic constraint that the compiler enforces to allow the programmer to tell the compiler that a value is unchanged. If you do have a value in your programming that remains unchanged, you should explicitly use the const to get help from the compiler. 1.const modifier member Variable

A const can be used to modify a member variable to tell the compiler that the member variable remains unchanged. Particular attention should be paid to the const modifier pointer variable:

1 There is only one const, if the const is on the left of *, the data pointed to by the pointer is a constant and cannot be modified by the dereference; The pointer itself is a variable and can point to other memory cells.

2 There is only one const, if the const is on the right side, the pointer itself is a constant, cannot point to another memory address, the data that the pointer refers to can be modified by the dereference.

3) Two const,* or so, then the data that the pointer and the pointer cannot modify.

Examples are as follows:

int main ()
{
	int a1=3;  Non-const data
	const int A2=A1;	Const data
	int *a3=&a1;		Non-const data,non-const pointer
	const INT *a4=&a1;	Const DATA,NON-CONST Pointer
	int * CONST a5=&a1;	Non-const data,const pointer
	const INT * Const a6=&a1;		Const DATA,CONST Pointer
	const INT * Const a7=&a1;		Const DATA,CONST pointer return
	0;
}
2.const modifier function Parameters

The passed arguments cannot be changed within the function as they were when the variable was modified.

For example:

void Testmodifyconst (const int _x)
{
	_x=5;
}

When you compile the above function, you will get an error. Because the _x is immutable. 3.const decorated member functions

1 A const-decorated member function cannot modify any member variable except for mutable-modified variables.

2 A const member function cannot invoke a non-const member function because a non-const member function might modify a member variable.

For example:

#include <iostream>
using namespace std;
Class point{public
: Point
(int _x): X (_x) {}
void testconstfunction (int _x) const{///
error, in const member function , you cannot modify any class member variable
x=_x;
Error, the const member function cannot invoke a ONST member function because a non-const member function can modify a member variable.
modify_x (_x);
}
void modify_x (int _x) {
   	x=_x;
}
int x;
};
4.const decorated member functions

1) pointer passing

If you return the const Data,non-constpointer, the return value must also be assigned to the const data,non-const pointer. Because the data that the pointer points to is a constant that cannot be modified.

For example:

const int * MALLOCA ()	///const data,non-const pointer
{
	int *a=new int (2);
	return A;
}
int main ()
{
	const int *a=malloca ();
	int *b=malloca ();    Compilation error return
	0;
}

2) Value Delivery

If the function return value takes a "value pass", the const modifier has no value because the function copies the return value to the external temporary storage cell. So, for value transfer, adding a const doesn't mean much. 5. Examples of pen questions

1) 2014 Perfect World Choice question 10th question

The print () function is a regular member function of a class that has no return value, and the following representation is correct: A

void print () const b.constvoid print () c.void constprint () d.void print (const)

Parsing: A const is used to modify the member function, B is used to modify the function return value, c No such use, D is the use of the modifier function passed the parameter value.

2) 2013 Perfect World Choice question 6th question

struct Thing

{

int Valuea;

const int VALUEB;

};

Thing T;

The values of the member variables Valuea and VALUEB of T are respectively: C
A, 0 0 B, garbage value 0 C, unable to run D, garbage value garbage value

Parsing: Valueb is a const type and must be initialized before it can be done.



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.