473,386 Members | 1,830 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,386 software developers and data experts.

Operator overloading

Before you all start flaming me, I am not a student and this is not
for any homework. Just someone learing c++ on their own.
I am now up to the chapter in my book that describes operator
overloading.

I just cannot find any explanation that clearly point out what the
parts of the statement refer to. For example the book says:

comp operator+(comp b)

is the same as

a . operator+ (b)

I dont understand how is it a.operator, I wish there wasa graphical
explanation that shows what each component represents. I have searched
many sites, and found a lot of sites with Operator overloading
articles, but NONE give a clear explanation to me, maybe I am just not
getting it. Anyone care to share any sites that can help me out or
give me a shot with their explanation. And by the way, if I have
posted to the wrong group please forgive me, I have seen so many
people get chewed out for asking a question here that I am sometimes
hesitant to post. I know some questions rightfully dont belong here, I
hope I am in the right place. For all those that take the time to
answer my question, I thank you all in advance for your time and
attention.

vi*********@yahoo.com
if you want to directly email me.

Jul 19 '05 #1
2 5842

<vi*********@yahoo.com> wrote in message
news:n3********************************@4ax.com...
Before you all start flaming me, I am not a student and this is not
for any homework. Just someone learing c++ on their own.
I am now up to the chapter in my book that describes operator
overloading.

I just cannot find any explanation that clearly point out what the
parts of the statement refer to. For example the book says:

comp operator+(comp b)

is the same as

a . operator+ (b)
They aren't the same. The first one looks like a member declaration, the
second one is a member function call.

I dont understand how is it a.operator, I wish there wasa graphical
explanation that shows what each component represents.
Component? When you overload operators, you have to write a function and you
have to give that function a names. If you overload + then the name is of
the function is operator+.

a + b

is short for either

a.operator+(b)

or

operator+(a, b)
I have searched
many sites, and found a lot of sites with Operator overloading
articles, but NONE give a clear explanation to me, maybe I am just not
getting it. Anyone care to share any sites that can help me out or
give me a shot with their explanation. And by the way, if I have
posted to the wrong group please forgive me, I have seen so many
people get chewed out for asking a question here that I am sometimes
hesitant to post. I know some questions rightfully dont belong here, I
hope I am in the right place. For all those that take the time to
answer my question, I thank you all in advance for your time and
attention.


Its the right group, your question is entirely about the C++ language.

Maybe what you are getting at is this. Given the expression

a + b

where a and b are objects (of type A say) the compiler can interpret that
two ways. It can call a global function, like this

operator+(a, b)

That's just like a regular function call, but it has a strange name. Or it
can call a member function like this

a.operator+(b)

again this is just like any other member function call except for the
strange name.

So which one happens, which way does the compiler interpret it? It all
depends one what you have declared for operator+. If you have written
something like this

A operator+(A a, A b)
{
...
}

then the compiler is going to treat a + b as a global function call. On the
other hand if you have written something like this

class A
{
public:
A operator+(A b)
{
...
}
};

then the compiler is going to treat a + b as a member function call.

Thats all there is to it.

HTH
john
Jul 19 '05 #2
<vi*********@yahoo.com> wrote in message
news:n3********************************@4ax.com...
Before you all start flaming me, I am not a student and this is not
for any homework.
No need to be so defensive. Your question is a reasonable one.
Just someone learing c++ on their own.
I am now up to the chapter in my book that describes operator
overloading.

I just cannot find any explanation that clearly point out what the
parts of the statement refer to. For example the book says:

comp operator+(comp b)
operator+ requires two arguments (because it adds two values together), so
this looks like a declaration from the definition of class comp.

class comp
{
public:
// members
comp operator+(comp b);
// more members
};

The object you call the function for is the first argument, and b is the
second argument.
is the same as

a . operator+ (b)
This is where you call the operator declared above:
comp a(1); // assume constructor with numerical arg
comp b(2);
comp c = a.operator+(b); // 1 + 2

This computes a + b (1 + 2) and stores the result in c.
I dont understand how is it a.operator,
It's an operator because you normally use it like this:
c = a + b;
not:
c = a.operator+(b);

Your book should discuss the a + b form somewhere, because being able to
write a + b is the reason you would choose to overload operator+. If you are
only going to call it as a function you might as well just have a function
called 'add' and call it like this: a.add(b);
I wish there wasa graphical
explanation that shows what each component represents.

I have searched
many sites, and found a lot of sites with Operator overloading
articles, but NONE give a clear explanation to me, maybe I am just not
getting it. Anyone care to share any sites that can help me out or
give me a shot with their explanation. And by the way, if I have
posted to the wrong group please forgive me, I have seen so many
people get chewed out for asking a question here that I am sometimes
hesitant to post.
They get chewed out for asking off-topic questions. Your question is
on-topic.
I know some questions rightfully dont belong here, I
hope I am in the right place.


Yes, you are.

BTW, these kinds of operator overloads are normally implemented as global
functions instead of as class member functions, because there are advantages
in using global functions. I hope your book covers that.

DW

Jul 19 '05 #3

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

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
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...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
by: Jerry Fleming | last post by:
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: (), ,...
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...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.