How to use const_cast?

前端 未结 2 1776
遇见更好的自我
遇见更好的自我 2020-11-27 11:37

I have a private variable in my Student class defined as:

const int studentNumnber;

I am trying to write a copy constructor fo

2条回答
  •  隐瞒了意图╮
    2020-11-27 11:58

    In your code you are trying cast this pointer instead of variable. You can try the following:

    Student(const Student & s)
        : Person(p.getName(), p.getEmailAddress(), p.getBirthDate()), school(0), studentNumber(0) {
        school = new char[strlen(s.school) + 1];
        strcpy_s(school, strlen(s.school) + 1, s.school);
        *const_cast(&studentNumber) = s.studentNumber;
    }
    

提交回复
热议问题