The const modifier basic data types, pointers, references, objects __c++ in C + +

Source: Internet
Author: User
const modifier BASIC data Type
#include <iostream>
using namespace std;

void Main () {
	const int a = 1;
	const char b = ' k ';
	Const float c = 3.14f;
	A = 2;  
	b = ' n ';
	c = 1.2f;
}
The const modifier base type indicates that these types are constants and cannot be modified or assigned. It is also necessary to note that the 3.14 default is the double type, which should be written as float C = 3.14f if saved with a float variable.
Const modifier pointer
#include <iostream>
using namespace std;

void Main () {
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	const int *P1 =&a;   Variable pointer
	int const * P2 =&b;  pointing to shaping constants Variable pointer
    int * Const P3 =&c;  pointing to reshape constants Constant pointer to variable reshaping
	const int * const P4 =&d;  Constant pointer to reshape constants
}
The P1 is pointing to an integer constant, so the *p1=2 is wrong. But can carry on p1++ such operation, also can change P1 point of P2 and P1 just like the expression form is different, the same *p2=2; But it can be done p2++, also can change P2 point p3 is a constant pointer, p3 itself can not change, can not change the direction of P3, but it points to variable, *p3=2, is the P4 is a constant pointer, and point to the content of constants, *p4=2, is wrong.
Judging method: Const in * Before the description is a pointer to a constant, const after * description pointer is a constant pointer. * The const description pointer is a regular pointer to a constant.
Const modifier ReferenceA lot of students are just as obsessed as I am. Comparing the const modifier reference with the const modifier pointer is not four cases where the const-decorated reference also has a modifier pointer.
#include <iostream>
using namespace std;

void Main () {
	int a1 = 1;
	int a2 = 1;
	int a3 = 1;
	int a4 = 1;
	const INT & b1 = A1;
	int const & b2 = A2;
	int & Const B3 = A3;  Prompt "Qualifiers on reference are ignored"
	const int & const B4 = A4;  Prompt "Qualifiers on reference are ignored"
}
const INT & b1 = A1; Then B1 is a constant b1++, b1=2 is not possible, although B1 is a A1 alias, but not B1 to modify A1. However, A1 is not a constant, and the value that can be A1=2,B1 will change accordingly. int const & b2 = A2; the same as the first, B2 is a reference constant and cannot modify A2 through B2, but A2 is not a constant and can be modified, b2 changes. int & Const B3 = A3 prompted "Qualifiers on reference are ignored", that is, the qualifier for the reference is ignored, so the equivalent of int & b3 = A3; The const does not work. const int & const B4 = A4; Empathy is essentially the same as the first, equivalent to the const int & b4 = A4; Summary: First of all, you need to know how to initialize when you define a reference, and the reference cannot change the point. The const then indicates that the reference is a constant reference before &, and the value cannot be modified by the reference. The const has no effect after &.
Const Modifier Object
#include <iostream>
using namespace std;

Class A {
private:
	int i;
Public:
	A () {i = 0;}
	void set (int n) {
		i = n;
	}
	The value of the Void get () const {
		cout<< member variable: ' <<i<<endl;
	}
};
void Main () {
	const A;
	A.set (1);
	A.get ();
}

Defines a const object in which the Const object attempts to invoke the SET function to change the value of a member variable, which is not allowed. The const object, as its name implies, is a constant object, and the member variables of all objects are immutable. Also, the Const object cannot call a non-const function, even if the set function does not modify the member variable, and the compilation fails. And the Get function is a const function, so the const object can call the Take function.

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.