473,385 Members | 1,356 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,385 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 3593
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,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.