1

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;
6
  • 2
    No, you cannot alter the meaning of operators as applied to built-in types. Pointers are built-in types. What is the real problem you are trying to solve? This looks very much like an XY problem. Commented Jan 26, 2018 at 1:26
  • I was wondering if I can implement smart pointer by inheritance, which requires to overload the pointer assignment. Seems like it's not possible and it has to be done as a class by itself Commented Jan 26, 2018 at 1:31
  • By inheritance of what from what else? Why does it require overloading the pointer assignment? Commented Jan 26, 2018 at 1:32
  • I was thinking if I can create smartPtr class and if other classes want to use smart pointer it just inherit from smartPtr. This way smartPtr class doesn't have to appear in the code like the old way (smartPtr<targetClass>) Commented Jan 26, 2018 at 1:36
  • If you write a smartPtr class or class template, you can define the behavior of variables declared using smartPtr nearly any way you want. But no, there is no way for such a thing to change the behavior of variables declared as A*: that is and will always be the classic "dumb" pointer type. Commented Jan 26, 2018 at 1:46

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

No, because overloaded operators are only considered when at least one argument has a class or enum type. (A pointer to class type is considered a fundamental type and does not count as a "class type" itself.)

C++ Standard [over.match.oper]/1-2:

If no operand of an operator in an expression has a type that is a class or an enumeration, the operator is assumed to be a built-in operator and interpreted according to Clause [expr].

If either operand has a type that is a class or enumeration, a user-defined operator function might be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator. In this case, overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator.

(Though you might instead encourage users of A to use some smart pointer-like class APtr instead of raw pointers to get the desired behavior.)

Comments

0

There is no "pointer assignment operator". This is the assignment operator and you are using it with pointer operands.

It's not possible to overload an operator unless at least one operand has class or enum type. (Pointer type is not class type).

2 Comments

... or enum type.
@IgorTandetnik fixed, ty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.