Vector of structs with const members?

后端 未结 4 1204
有刺的猬
有刺的猬 2021-01-11 16:07

Let\'s say I have

#include 
#include 
using namespace std;

struct Student
{
    const string name;
    int grade;
    Student(co         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-11 16:46

    Elements of vectors must be copy-assignable, which your Student struct isn't because of the const member. Simply use string name instead of const string name. Unless you have a specific requirement, constant members in classes are seldom useful. If you want to prevent changes to the member, make it private and add a public getter function.

提交回复
热议问题