operator overloading for primative types

P

PengYu.UT

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);
}
 
K

Kai-Uwe Bux

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
 
I

Ian Collins

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.
 
M

Me

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. :^)
 
G

Guest

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]
 
G

Guest

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.
 
J

Juha Nieminen

Erik said:
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

Ian Collins

Juha said:
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.
 
K

Kai-Uwe Bux

Juha said:
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
 
A

Alf P. Steinbach

* 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)
 
K

Kai-Uwe Bux

Alf said:
* Kai-Uwe Bux:
^^^^^^^

Heya! :)



^^^^^^^

:) :) :)
Thanks.

What an interesting instance of the liars paradox: had you left out this
sentence, it would have been true. :)


Best

Kai-Uwe Bux
 
G

Guest

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.
 
O

osmium

Juha Nieminen said:
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.
 
J

James Kanze

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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top