0
点赞
收藏
分享

微信扫一扫

结构体中const使用场景

90哦吼 2022-01-27 阅读 71
c++

作用:用const来防止误操作

eg:

#include<iostream>
#include<string>
using namespace std;
struct student
{
    string name;
    int age;
    int score;
};
void printstudent(const student* stu)
{
    //stu->age=100;//操作失败,因为加了const修饰
    cout << "姓名:" << stu->name << "  年龄:" << stu->age << "  分数:" << stu->score << endl;
}
int main()
{
    student stu = { "张",18,100 };
    printstudent(&stu);
}

举报

相关推荐

0 条评论