overloading operator

J

Jerry Fleming

As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), [], ->, =. I wonder
why there is such a restriction.

Some tutorials say that 'new' and 'delete' can only be overloaded with
static member functions, others say that all overloading function should
be non-static. Then what is the fact, and why?

I'd appreciate it very much if anyone can answer me or lead me to a
detail explanation.
 
I

Ian Collins

Jerry said:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), [], ->, =. I wonder
why there is such a restriction.
If there wasn't, how could you specify the left hand side of an expression?

All unary operators have to be class members.
Some tutorials say that 'new' and 'delete' can only be overloaded with
static member functions, others say that all overloading function should
be non-static. Then what is the fact, and why?
You can provide a global operator new(), or a class specific one which
has to be a class member.
I'd appreciate it very much if anyone can answer me or lead me to a
detail explanation.

All this is well covered in Stroustrup.
 
K

Kouisawang

Ian said:
Jerry said:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), [], ->, =. I wonder
why there is such a restriction.
If there wasn't, how could you specify the left hand side of an expression?

All unary operators have to be class members.

Not really, you can do overload unary "-" or binary "-" by using a
friend function.
 
A

Alf P. Steinbach

* Ian Collins:
Jerry said:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), [], ->, =. I wonder
why there is such a restriction.
If there wasn't, how could you specify the left hand side of an expression?

In the same way as with other operators: as an argument.

E.g., if it weren't for the language restriction,

MyClass& operator=( MyClass& lhs, MyClass const& rhs ) { ... }

I don't know why that isn't allowed, but one common feature is that
these operators are not meaningful for enum types, only for class types.

All unary operators have to be class members.

Sorry, that's incorrect.

§13.5.1 "A prefix unary operator shall be implemented by a non-static
member function (9.3) with no parameters or a non-member function with
one parameter. Thus, for any prefix unary operator @, @x can be
interpreted as either x.operator@ or operator@(x)."

And ditto for postfix ones.
 
I

Ian Collins

Alf said:
* Ian Collins:
Jerry said:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), [], ->, =. I wonder
why there is such a restriction.
All unary operators have to be class members.


Sorry, that's incorrect.

§13.5.1 "A prefix unary operator shall be implemented by a non-static
member function (9.3) with no parameters or a non-member function with
one parameter. Thus, for any prefix unary operator @, @x can be
interpreted as either x.operator@ or operator@(x)."

And ditto for postfix ones.
Oops, thanks for the correction.
 
J

Jerry Fleming

Jerry said:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), [], ->, =. I wonder
why there is such a restriction.
If there wasn't, how could you specify the left hand side of an expression?

All unary operators have to be class members.
Some tutorials say that 'new' and 'delete' can only be overloaded with
static member functions, others say that all overloading function should
be non-static. Then what is the fact, and why?
You can provide a global operator new(), or a class specific one which
has to be a class member.
I'd appreciate it very much if anyone can answer me or lead me to a
detail explanation.

All this is well covered in Stroustrup.
Thanks everybody. I have just find a post on overloading =, and that
seems to make it clear: the four operators are disallowed for
overloading with friends because otherwise they might lead to error. To
quote Stroustrup:

"Only a few assumptions are made about the meaning of a userdefined
operator. In particular, operator=, operator[], operator(), and
operator> must be non-static member functions; this
ensures that their first operands will be lvalues (§4.9.6)."

As to the question of "new" and "delete", they are implicitly static if
defined within a class.

Am I right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,272
Latest member
MaricruzDu

Latest Threads

Top