473,498 Members | 1,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator overloading for primative types

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
15 1715
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
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
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
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
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
Pe*******@gmail.com wrote:
primative
What's that?
Sep 29 '07 #7
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
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
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
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.
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
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
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
"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
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
On Sep 30, 1:23 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
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.
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 orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 1 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2558
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,...
6
2721
by: sandSpiderX | last post by:
Hi, How do i use this definition of overloaded operator, T& operator*(T*); like struct X {}; X ox; X* px=&ox;
17
2475
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
6
5380
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
3
2265
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?
3
3248
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,...
14
2781
by: Jess | last post by:
Hi, I read about operator overloading and have a question regarding "operator->()". If I have two classes like this: struct A{ void f(); }; struct B{
8
2948
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,...
24
1883
by: Rahul | last post by:
Hi Everyone, I was just overloading operator = for a class and i have a problem in one case... class A { A& operator=(const A& obj) { return *this;
0
6998
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...
0
7200
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...
1
6884
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...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
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,...
1
4904
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...
0
4586
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.