专业编程基础技术教程

网站首页 > 基础教程 正文

const in C++

ccvgpt 2024-08-08 12:56:06 基础教程 10 ℃

C++里的const修饰符,可以修饰一般变量,也可以修饰指针,还可以修饰成员函数。const修饰符的作用,表示某些量不能被修改(modify)。

下面用C++代码说明:

const in C++

#include <iostream>

using namespace std;

void const_int();

void int_ptr();

void const_int_ptr();

void int_ptr_const();

void const__int_ptr_const();

void m_function_const();

int main()

{

cout << "const是一个左结合的类型修饰符" <<endl<<"-----------------------------"<<endl;

const_int();

const_int_ptr();

int_ptr_const();

const__int_ptr_const();;

m_function_const();

return 0;

}


void const_int()

{

//a是一个常整型数。

const int a = 90;

cout << "const int a = 90 : a是常整型量。 " << endl;

cout << "a = " << a << endl<<"-----------------------------"<<endl;

}


void const_int_ptr()

{

/*--------- const int* ----------*/

//a是一个指向常整型数的指针, 整型数是不可修改的,但指针可以重新指向新的变量。

//content cannot be modified, pointer itself can be modified.

const int* a = new int(11);

cout << "const int* a = new int(11): " << endl;

cout << "指向常整型量的指针 a. " << endl;

cout << "指针的内容不能被修改," << endl;

cout << "但是指针变量本身可以被修改。" << endl;

cout << " *a= " << *a << endl << "-----------------------------" << endl;

delete a; // const in * 与int const* 等价

}


void int_ptr_const()

{

/*------------ int * const --------------*/

//pointer cannot be modified. content of pointer can be modified.

int* const a = new int(22);

cout << "int* const a = new int(22)," << endl;

cout << "a是一个指向整型数的常指针," << endl;

cout << "也就是说,指针的conten是可以修改的," << endl;

cout<< "但指针是不可修改的."<<endl;

cout << "*a = " << *a << endl;

*a = 2288;

cout << "执行语句 *a = 2288 之后:" << endl;

cout << "*a = " << *a<<endl << "-----------------------------" << endl;

delete a;

}


void const__int_ptr_const()

{

/*----------const int * const -----------*/

/* a是一个指向常整型数的常指针,

指针指向的整型 数是不可修改的,

同时指针也是不可修改的。

*/

const int* const a = new int(33);

cout << "const int* const a = new int(33)," << endl;

cout << "const int* const a : 指向常整型数的常指针a. " << endl;

cout << "指针和指针的内容都是不可以修改的。" << endl;

cout << "*a = " << *a << endl << "-----------------------------" << endl;;

delete a;

}


void m_function_const()

{

string str;

str += "void classname::function( )const;\n";

str += "成员函数后面的const, 表示是常成员函数,\n";

str += "常成员函数不会改变成员变量值。\n";

cout << str << endl;

}


void int_ptr()

{

int* a = new int(22);

*a = 122; //the content of pointer can be changed

int b = 11;

a = &b; //the pointer can be changed too.

delete a; //free memory on heap maually.

}


程序输出结果:

const是一个左结合的类型修饰符

-----------------------------

const int a = 90 : a是常整型量。

a = 90

-----------------------------

const int* a = new int(11):

指向常整型量的指针 a.

指针的内容不能被修改,

但是指针变量本身可以被修改。

*a= 11

-----------------------------

int* const a = new int(22),

a是一个指向整型数的常指针,

也就是说,指针的conten是可以修改的,

但指针是不可修改的.

*a = 22

执行语句 *a = 2288 之后:

*a = 2288

-----------------------------

const int* const a = new int(33),

const int* const a : 指向常整型数的常指针a.

指针和指针的内容都是不可以修改的。

*a = 33

-----------------------------

void classname::function( )const;

成员函数后面的const, 表示是常成员函数,

常成员函数不会改变成员变量值。

【结语】本文用C++代码方式,说明了修饰符const用在普通变量、指针、成员函数上的不同涵义。const指针的应用,让程序变得更加清晰易读,是C++中常见的修饰符。掌握这些修饰符,对于新手阅读范例代码,很有帮助。

Tags:

最近发表
标签列表