Vector of structs with const members?

后端 未结 4 1250
有刺的猬
有刺的猬 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:48

    The simple answer is: you can't. If you have const member variables, then the compiler can't supply a default copy-assignment operator. However, many of the operations that std::vector provides need to make assignments, and therefore require a (public) copy-assignment operator.

    Your options are:

    1. Make name non-const.
    2. Write your own copy-assignment operator, and think of a way to deal with "copying" a const member.

提交回复
热议问题