c++ pointer assignment operator overload(not just object assignment but pointer assignment)

后端 未结 3 1852
灰色年华
灰色年华 2020-12-12 05:27

Is there a way to overload the pointer assignment operator? e.g. overload pointer assignment operator for class A when

A *x, *y;
x = y;
3条回答
  •  被撕碎了的回忆
    2020-12-12 05:42

    No, this is not possible. Pointer types are scalar types, but overloading operators requires a non-scalar parameter. In particular ([over.oper]p6):

    An operator function shall either be a non-static member function or be a non-member function that has at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.

    If you had a binary operator where one parameter was a user-defined type and the other a pointer, that would work, but in the situation you're asking about, both operands are pointers.

提交回复
热议问题