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);
} 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 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.
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
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. :^)
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
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
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.
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.
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
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
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
"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.
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
;-)
>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,...
|
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;
|
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:
...
|
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,...
|
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?
| |
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,...
|
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{
|
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,...
|
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;
|
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...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |