Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between user-defined conversion and user-defined operator?

Tags:

c++

In the context of operator overloading, what is the difference between user-defined conversion and user-defined operator?

like image 620
user630983 Avatar asked Jul 24 '26 05:07

user630983


2 Answers

A user-defined conversion is either:

  • A constructor in the destination type which can be called with a single parameter of the source type (more arguments can exist if they have default values)

or

  • A non-static member function of the source type with the name operator DESTTYPE()

As you can see, the second option uses the operator keyword just as is used when overloading the traditional operators.

(Note: All of this is formally described in section [class.conv] of the C++ standard.)

like image 161
Ben Voigt Avatar answered Jul 28 '26 06:07

Ben Voigt


In C++, there is no concept of user-defined operators that can be overloadable. Only existing operators, with an exception of few( ., .*, ::, ?:, sizeof ), can be overloaded.

like image 45
Mahesh Avatar answered Jul 28 '26 06:07

Mahesh