CC++中如何高效使用CONST关键字?

2026-05-20 01:040阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1400个文字,预计阅读时间需要6分钟。

C/C++中如何高效使用CONST关键字?

1. 修饰常量时: - `const int temp1;` // temp1为常量,不可变 - `int const temp2;` // temp2为常量,不可变

2. 修饰指针时: - 主要看`const`在`*`的前后,在前则指针指向的内容为常量,在后则指针本身为常量。

C/C++中如何高效使用CONST关键字?

1、修饰常量时:

const int temp1; //temp1为常量,不可变   int const temp2; //temp2为常量,不可变

2、修饰指针时:

  主要看const在*的前后,在前则指针指向的内容为常量,在后则指针本身为常量;

const int *ptr; //*ptr为常量;   int const *ptr; //*ptr为常量;   int* const ptr; //ptr为常量;   const int * const ptr; //*ptr、ptr均为常量;

3、const修饰类对象时:

  const修饰类对象时,其对象中的任何成员都不能被修改。const修饰的对象,该对象的任何非const成员函数都不能调用该对象,因为任何非const成员函数都会有修改成员变量的可能。

阅读全文

本文共计1400个文字,预计阅读时间需要6分钟。

C/C++中如何高效使用CONST关键字?

1. 修饰常量时: - `const int temp1;` // temp1为常量,不可变 - `int const temp2;` // temp2为常量,不可变

2. 修饰指针时: - 主要看`const`在`*`的前后,在前则指针指向的内容为常量,在后则指针本身为常量。

C/C++中如何高效使用CONST关键字?

1、修饰常量时:

const int temp1; //temp1为常量,不可变   int const temp2; //temp2为常量,不可变

2、修饰指针时:

  主要看const在*的前后,在前则指针指向的内容为常量,在后则指针本身为常量;

const int *ptr; //*ptr为常量;   int const *ptr; //*ptr为常量;   int* const ptr; //ptr为常量;   const int * const ptr; //*ptr、ptr均为常量;

3、const修饰类对象时:

  const修饰类对象时,其对象中的任何成员都不能被修改。const修饰的对象,该对象的任何非const成员函数都不能调用该对象,因为任何非const成员函数都会有修改成员变量的可能。

阅读全文