473,788 Members | 3,068 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 5885

<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
2621
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 operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
34
6472
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: Complex( float = 0.0, float = 0.0 );
16
3097
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 'adding' two objects will sum up the corresponding int members.
2
2067
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
8671
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 language offers a primitive exp operator.
3
2301
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
3630
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, others say that all overloading function should be non-static. Then what is the fact, and why? ...
3
3283
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
3515
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 us to get rid of all the temporary arrays produced by NumPy. For example, consider the...
8
2976
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 these operators must be nonstatic member functions, but why this ensure their first operands will...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.