overloading assignment operator With subscript operator

后端 未结 5 446
忘了有多久
忘了有多久 2020-12-19 16:31

I overloaded both subscript operator and assignment operator and I am trying to get right value to assignment operator example Array x; x[0]=5; by overloading

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 17:19

    You overloaded operator= for the Array class, not for int (which you can't do, by the way). x[1]=5; is using Array::operator[] for element access, which returns an int&, and then uses the normal int assignment operator for the =5 part. Array::operator= is only used when you're assigning to the whole object (i.e. the way you've defined it, you can do x = 5;), not to its elements/members.

提交回复
热议问题