473,241 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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.
Oct 23 '06 #1
5 3581
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.
Oct 23 '06 #2

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.
Oct 23 '06 #3
* 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?
Oct 23 '06 #4
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.
Oct 23 '06 #5
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
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?
Oct 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.