multplicaon operator


can we use multiplication operator as * as unary operator?
That is the dereference operator. It can be used on pointers to get what the pointers point to. It can also be overloaded to do whatever you want.
The first question has to be, what is unary multiplication?

That aside, the * already has a meaning when used on its own: pointer dereferencing.

int i = *pi; // some int* value

It is a convention that you should, in the majority of cases, only overload operators to provide the same operation for new types. So the unary operator* should always be about dereferencing.

Preserve natural semantics for overloaded operators

Summary

Programmers hate surprises: Overload operators only for good reason, and preserve natural semantics; if that's difficult, you might be misusing operator overloading.

(From Sutter and Alexandrescu, "C++ Coding Standards")

So you can overload the unary operator* (as a prefix operator), but should be wary about how you do so.

Do you have a particular use in mind?

Andy

PS There are exceptions. For example, Boost.Spirit uses operator* for the Kleene star, which works as * has a well known meaning in the context of this library (BNF notation)
Last edited on
Topic archived. No new replies allowed.