Operator = Overload with Const Variable in C++

前端 未结 6 1112
刺人心
刺人心 2020-12-31 23:48

I was wondering if you guys could help me.

Here are my .h:

Class Doctor {
   const string name;
   public:
       Doctor();
       Doctor(string nam         


        
6条回答
  •  温柔的废话
    2021-01-01 00:33

    As many have said earlier, the member is 'const' indicates that it has to be initialized during its creation and it is not supposed to change. If at all you have to write an assignment operator for such cases and you cannot skip the assignment of that member variable, then make it 'mutable'.

    Remember; from the C++ standard, "casting away constness of an originally declared const variable is undefined behaviour."

    HTH, Abhay

提交回复
热议问题