结构体中const使用场景

  • 作用:用const来防止误操作

代码示例:

#include <iostream>
#include <string>
using namespace std;
//const的使用场景
struct student
{
   
       //姓名
       string name;
       //年龄
       int age;
       //分数
       int score;
};
//将函数中的形参改为指针,可以减少内存空间
void printStudents( const student *s)
{
   
       //s->age = 150;//加入const之后,一旦有修改的操作符就会报错,而且不会复制新的副本出来
       cout << "姓名:" << s->name
              << " 年龄:" << s->age
              << " 分数:" << s->score << endl;
}
int main()
{
   
       //创建结构体变量
       struct student s={
   "张三",15,70};
       //通过函数打印结构体变量信息
       printStudents(&s);
       cout << "main函数中张三姓名:" << s.name
              << " 年龄:" << s.age
              << " 分数:" << s.score << endl;
       return 0;
}