Operator overloading

V

victor75040

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.

(e-mail address removed)
if you want to directly email me.
 
J

John Harrison

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
 
D

David White

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top