By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Operator overloading

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

<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

P: n/a
<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 discussion thread is closed

Replies have been disabled for this discussion.