473,287 Members | 1,899 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,287 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 3585
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,...
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
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
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
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
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
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.