| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Jerry Fleming" |
| Date: |
23 Oct 2006 12:01:00 AM |
| Object: |
overloading operator |
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.
.
|
|
| User: "Ian Collins" |
|
| Title: Re: overloading operator |
23 Oct 2006 01:50:35 AM |
|
|
Jerry Fleming wrote:
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.
--
Ian Collins.
.
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: overloading operator |
23 Oct 2006 03:52:13 AM |
|
|
* Ian Collins:
Jerry Fleming wrote:
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.
--
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?
.
|
|
|
| User: "Ian Collins" |
|
| Title: Re: overloading operator |
23 Oct 2006 04:19:24 AM |
|
|
Alf P. Steinbach wrote:
* Ian Collins:
Jerry Fleming wrote:
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.
--
Ian Collins.
.
|
|
|
|
|
| User: "Kouisawang" |
|
| Title: Re: overloading operator |
23 Oct 2006 02:05:00 AM |
|
|
Ian Collins wrote:
Jerry Fleming wrote:
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.
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.
--
Ian Collins.
.
|
|
|
|
| User: "Jerry Fleming" |
|
| Title: Re: overloading operator |
23 Oct 2006 08:14:23 PM |
|
|
On 2006-10-23 14:50, Ian Collins wrote:
Jerry Fleming wrote:
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?
.
|
|
|
|
|

|
Related Articles |
|
|