473,569 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*********@yah oo.com
if you want to directly email me.

Jul 19 '05 #1
2 5863

<vi*********@ya hoo.com> wrote in message
news:n3******** *************** *********@4ax.c om...
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*********@ya hoo.com> wrote in message
news:n3******** *************** *********@4ax.c om...
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
2584
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, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign...
34
6412
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 operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public:...
16
3071
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 has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that...
2
2052
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 overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
67
8585
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 in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither...
3
2282
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
3612
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: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions,...
3
3263
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, obj2 ;
9
3487
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 allowing the assignment operator (=) to be overloaded. One particular use for this would be to implement "lazy evaluation". For example it would allow...
8
2961
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, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.