C++基礎之結構體中const的使用場景

說明:用const來防止誤操作。

#include <iostream>
using namespace std;
#include<string>

struct Student
{
	string name;
	int age;
	int score;
};

void printStudents(const struct Student *s1)
{
	//s1->age = 19;//編譯報錯,因爲形參有const修飾所以不可修改,只能打印(讀)

	cout << s1->age << s1->name<<s1->score<<endl;
}

int main() 
{
	struct Student s1 = { "小紅",14,67 };

	printStudents(&s1);

	system("pause");
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章