 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jerry Fleming Guest
|
Posted: Mon Oct 23, 2006 9:10 am Post subject: 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. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Mon Oct 23, 2006 9:10 am Post subject: 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. |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Mon Oct 23, 2006 9:10 am Post subject: Re: overloading operator |
|
|
* Ian Collins:
| Quote: | 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.
| 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? |
|
| Back to top |
|
 |
Kouisawang Guest
|
Posted: Mon Oct 23, 2006 9:11 am Post subject: Re: overloading operator |
|
|
Ian Collins wrote:
| Quote: | 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.
| 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.
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. |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|