Connecting Tech Pros Worldwide Forums | Help | Site Map

overloading operator

Jerry Fleming
Guest
 
Posts: n/a
#1: Oct 23 '06
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.

Ian Collins
Guest
 
Posts: n/a
#2: Oct 23 '06

re: overloading operator


Jerry Fleming wrote:
Quote:
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.
Quote:
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.
Quote:
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.

--
Ian Collins.
Kouisawang
Guest
 
Posts: n/a
#3: Oct 23 '06

re: overloading operator



Ian Collins wrote:
Quote:
Jerry Fleming wrote:
Quote:
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.
Quote:
>
Quote:
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.
>
Quote:
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.
>
--
Ian Collins.
Alf P. Steinbach
Guest
 
Posts: n/a
#4: Oct 23 '06

re: overloading operator


* Ian Collins:
Quote:
Jerry Fleming wrote:
Quote:
>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.

Quote:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Ian Collins
Guest
 
Posts: n/a
#5: Oct 23 '06

re: overloading operator


Alf P. Steinbach wrote:
Quote:
* Ian Collins:
>
Quote:
>Jerry Fleming wrote:
>>
Quote:
>>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.
>>>
>
Quote:
>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.

--
Ian Collins.
Jerry Fleming
Guest
 
Posts: n/a
#6: Oct 24 '06

re: overloading operator


On 2006-10-23 14:50, Ian Collins wrote:
Quote:
Jerry Fleming wrote:
Quote:
>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.
>
Quote:
>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.
>
Quote:
>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
operatormust 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?
Closed Thread