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

operator overloading for primative types

P: n/a
Hi,

It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I'm wondering whether it is ture or there is
some walk-around?

Thanks,
Peng

#include <iostream>
#include <complex>

namespace my
{
int operator/(int x, int y) {
if(x>0)
return x/y;
else
return x/y - 1;
}

void test(int x, int y) {
std::cout << x << "/" << y << " " << x/y << std::endl;
}

}

int main() {
my::test(10, 3);
my::test(-10, 3);
my::test(10, -3);
my::test(-10, -3);
}

Sep 27 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Pe*******@gmail.com wrote:
Hi,

It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I'm wondering whether it is ture
It is true. See clause [13.3.1.2/1]
If no operand of an operator in an expression has a type that is a class
or an enumeration, the operator is assumed to be a built-in operator and
interpreted according to clause 5.
or there is some walk-around?
I don't know of any that work transparently. You have to rig your operands
so that at least one of them is not of built-in type.
Best

Kai-Uwe Bux
Sep 27 '07 #2

P: n/a
Pe*******@gmail.com wrote:
Hi,

It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I'm wondering whether it is ture or there is
some walk-around?
Luckily you can't, doing so would produce code that doesn't behave as
others would expect it to, a nightmare to maintain.

If you want a type with specialised behavior, wrap it in a class with
the appropriate operators.

--
Ian Collins.
Sep 27 '07 #3

P: n/a
On Sep 26, 10:51 pm, Ian Collins <ian-n...@hotmail.comwrote:
PengYu...@gmail.com wrote:
Hi,
It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I'm wondering whether it is ture or there is
some walk-around?

Luckily you can't, doing so would produce code that doesn't behave as
others would expect it to, a nightmare to maintain.
I agree. Some people are advocating something similar should be done
by introducing a std::intrusive::list type. I think it would be
better to make it ilist -
http://tinyurl.com/2q5p34
Brian Wood
Ebenezer Enterprises
www.webebenezer.net

Sep 27 '07 #4

P: n/a
Me
On Thu, 27 Sep 2007 15:51:26 +1200, Noone wrote:
Pe*******@gmail.com wrote:
>Hi,

It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I'm wondering whether it is ture or there is
some walk-around?
Luckily you can't, doing so would produce code that doesn't behave as
others would expect it to, a nightmare to maintain.

If you want a type with specialised behavior, wrap it in a class with
the appropriate operators.


Yes. I believe in the Macrosloth world they call that boxing:
encapsulating a primitive type in an object so that it can be manipulated
by other classes as a generic (Object), with whatever behavior the
programmer wants to implement. I pass no judgement on whether this is a
good thing or a bad thing. :^)

Sep 27 '07 #5

P: n/a
On 2007-09-28 00:07, Me wrote:
On Thu, 27 Sep 2007 15:51:26 +1200, Noone wrote:
>Pe*******@gmail.com wrote:
>>Hi,

It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I'm wondering whether it is ture or there is
some walk-around?
Luckily you can't, doing so would produce code that doesn't behave as
others would expect it to, a nightmare to maintain.

If you want a type with specialised behavior, wrap it in a class with
the appropriate operators.

Yes. I believe in the Macrosloth world they call that boxing:
encapsulating a primitive type in an object so that it can be manipulated
by other classes as a generic (Object), with whatever behavior the
programmer wants to implement. I pass no judgement on whether this is a
good thing or a bad thing. :^)
[Off topic]
The goal of boxing is to be able to place a value type on the managed
heap, so that it can be accessed by methods (and other things) that only
accept reference types. The equivalent in C++ would be something like this:

int a;
int* boxedA = new int(a);

One important thing to notice is that the boxed object and the non-boxed
are two separate objects with no connection what so ever (except having
the same value).

Specifically you can not take an int, box it, and then apply some user
defined operators on it. To do that you would have to create a value
type with overloaded operators and then box that. Which would be the
same as creating a new type in C++ and overloading its operators.

[/Off topic]

--
Erik Wikström
Sep 28 '07 #6

P: n/a
Pe*******@gmail.com wrote:
primative
What's that?
Sep 29 '07 #7

P: n/a
On 2007-09-29 09:33, Juha Nieminen wrote:
Pe*******@gmail.com wrote:
>primative

What's that?
It means that either your English is not good enough to understand what
the OP meant to say (in which case you could have asked in a more polite
way), or you did understand it and you are just being a jerk. I am
hoping it is the first one.

--
Erik Wikström
Sep 29 '07 #8

P: n/a
Erik Wikström wrote:
or you did understand it and you are just being a jerk.
In this society in which we live there's a rule of conduct which I
find the most peculiar and idiotic: It's considered impolite to remark
about other people's grammatical/spelling mistakes.

IMO this is the stupidest rule ever. If nobody tells you that you are
consistently writing something in the wrong way, how can you know that
you are writing in the wrong way? How can you learn the language if
nobody tells you what you are doing wrong? By being "polite" people are
only perpetuating the bad grammar instead of helping him learning.

There's at least one positive outcome in "being a jerk": If I myself
make a grammatical/spelling mistake, somebody is very likely to tell me
about it. This helps me learning to write better.
Sep 30 '07 #9

P: n/a
Juha Nieminen wrote:
>
There's at least one positive outcome in "being a jerk": If I myself
make a grammatical/spelling mistake, somebody is very likely to tell me
about it. This helps me learning to write better.
You asked for it - "I myself" is a tautology.

--
Ian Collins.
Sep 30 '07 #10

P: n/a
Juha Nieminen wrote:
Erik Wikstrm wrote:
>or you did understand it and you are just being a jerk.

In this society in which we live there's a rule of conduct which I
find the most peculiar and idiotic: It's considered impolite to remark
about other people's grammatical/spelling mistakes.

IMO this is the stupidest rule ever. If nobody tells you that you are
consistently writing something in the wrong way, how can you know that
you are writing in the wrong way? How can you learn the language if
nobody tells you what you are doing wrong? By being "polite" people are
only perpetuating the bad grammar instead of helping him learning.

There's at least one positive outcome in "being a jerk": If I myself
make a grammatical/spelling mistake, somebody is very likely to tell me
about it. This helps me learning to write better.
a) I know exactly what you are talking about. I am not a native speaker of
English living in the USA. Most people do not correct my mistakes, and it
makes it really hard to improve. Fortunately, I have a few friends whom I
was able to convice otherwise.

b) What made you appear like a jerk is not that you gave a correction; it's
that you chose to do so by droping into a conversation to which you had
nothing else to add.
Best

Kai-Uwe Bux
Sep 30 '07 #11

P: n/a
Alf P. Steinbach wrote:
* Kai-Uwe Bux:
>>
a) I know exactly what you are talking about. I am not a native speaker
of English living in the USA. Most people do not correct my mistakes, and
it makes it really hard to improve. Fortunately, I have a few friends
whom I was able to convice otherwise.
^^^^^^^

Heya! :-)
>b) What made you appear like a jerk is not that you gave a correction;
it's that you chose to do so by droping into a conversation to which you
had
^^^^^^^

:-) :-) :-)
Thanks.

>nothing else to add.
What an interesting instance of the liars paradox: had you left out this
sentence, it would have been true. :-)
Best

Kai-Uwe Bux
Sep 30 '07 #12

P: n/a
On 2007-09-30 10:42, Juha Nieminen wrote:
Erik Wikström wrote:
>or you did understand it and you are just being a jerk.

In this society in which we live there's a rule of conduct which I
find the most peculiar and idiotic: It's considered impolite to remark
about other people's grammatical/spelling mistakes.

IMO this is the stupidest rule ever. If nobody tells you that you are
consistently writing something in the wrong way, how can you know that
you are writing in the wrong way? How can you learn the language if
nobody tells you what you are doing wrong? By being "polite" people are
only perpetuating the bad grammar instead of helping him learning.

There's at least one positive outcome in "being a jerk": If I myself
make a grammatical/spelling mistake, somebody is very likely to tell me
about it. This helps me learning to write better.
I have nothing against pointing out people's spelling or grammar
mistakes, it was just your way of doing it and the fact that it was your
only contribution to the discussion. You could have just pointed out
that the OP spelt it wrong and then answered the question. Or just
answered the question making sure to include the correct spelling in
your reply and trust that the OP was smart enough to pick it up.

--
Erik Wikström
Sep 30 '07 #13

P: n/a
"Juha Nieminen" writes:
There's at least one positive outcome in "being a jerk": If I myself
make a grammatical/spelling mistake, somebody is very likely to tell me
about it. This helps me learning to write better.
I think it should be "This helps me learn to be a better writer".

I am not a grammarian but I stayed at a Holiday Inn.
Sep 30 '07 #14

P: n/a
Alf P. Steinbach wrote:
* Kai-Uwe Bux:
>>
a) I know exactly what you are talking about. I am not a native
speaker of
English living in the USA. Most people do not correct my mistakes, and it
makes it really hard to improve. Fortunately, I have a few friends whom I
was able to convice otherwise.
^^^^^^^

Heya! :-)
>b) What made you appear like a jerk is not that you gave a correction;
it's
that you chose to do so by droping into a conversation to which you had
^^^^^^^

:-) :-) :-)

>nothing else to add.

Cheers,

- Alf (speling master)
Nah, spelling master
;-)
>
Sep 30 '07 #15

P: n/a
On Sep 30, 1:23 pm, Erik Wikstrm <Erik-wikst...@telia.comwrote:
On 2007-09-30 10:42, Juha Nieminen wrote:
Erik Wikstrm wrote:
or you did understand it and you are just being a jerk.
In this society in which we live there's a rule of conduct which I
find the most peculiar and idiotic: It's considered impolite to remark
about other people's grammatical/spelling mistakes.
IMO this is the stupidest rule ever. If nobody tells you that you are
consistently writing something in the wrong way, how can you know that
you are writing in the wrong way? How can you learn the language if
nobody tells you what you are doing wrong? By being "polite" people are
only perpetuating the bad grammar instead of helping him learning.
There's at least one positive outcome in "being a jerk": If I myself
make a grammatical/spelling mistake, somebody is very likely to tell me
about it. This helps me learning to write better.
I have nothing against pointing out people's spelling or grammar
mistakes, it was just your way of doing it and the fact that it was your
only contribution to the discussion. You could have just pointed out
that the OP spelt it wrong and then answered the question. Or just
answered the question making sure to include the correct spelling in
your reply and trust that the OP was smart enough to pick it up.
Historically, at least, it was considered bad form to correct
spelling or other language issues publicly. You sent the poster
an email with the correction. Given the number of email
addresses mangled to prevent spam, I don't know if this is still
the case, but if the posting does have what appears to be a
valid email address, that's still the way I think it should be
handled.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Oct 1 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.