C++ introduced an interesting feature (among others): operator overloading.
The idea is to build a mechanism for the user defining its own number types and the operations to be
done with them.
I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350
bit floats.
Yes, 350. We have a lot of fast machines now. Why not use them? The extra-precision is useful in
some algorithms.
This extension to C is transparent, since it doesn't change anything in the basics of the language.
The syntax used is:
TYPE operator+(TYPE a,TYPE b)
{
}
When in your program you use
TYPE a,b;
...
a+b;
Means that you call the operator function with those two numbers. This is similar to C++, but since
there are no classes (C is not object oriented) everything is quite simple.
Note that this is an extension compatible with C and it is important to know that the C standard
doesn't forbid extensions. They should not introduce new keywords though.
In my implementation it means that
int operator = 6;
still works as it should. No new keywords.
The operator symbol is recognized if (and only if):
1) The level is global, i.e. not within a function definition.
2) It is followed by one of the defined operator symbols (+, -, etc)
3) This must be followed by a normal function definition. The name of this function is the sequence
of types in the argument list, the "signature" in technical terms.
Is this useful?
Yes, for numbers it is ideal. It is less so, when the data types aren't quantities but other
relationships or data. Take, for instance
String a,b,c;
...
c = a+b;
This means that a+b is different from b+a. Not very clear.
Operator overloding *can* be useful, specially for letting the users build new kinds of numbers,
that fulfill special needs.
What do you think?
Voice your opinion. In any case Lcc-win32 is available at: http://www.cs.virginia.edu/~lcc-win32 19 10169
On Fri, 08 Aug 2003 21:53:32 +0200, jacob navia wrote: C++ introduced an interesting feature (among others): operator overloading.
The idea is to build a mechanism for the user defining its own number types and the operations to be done with them.
I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350 bit floats.
[snip]
This is offtopic for comp.lang.c. comp.std.c is for discussion of changes
to the standard.
--
Freenet distribution not available
"Let's show this prehistoric bitch how we do things downtown!"
-- The Ghostbusters
bd wrote: I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350 bit floats.
[snip]
This is offtopic for comp.lang.c. comp.std.c is for discussion of changes to the standard.
How do you gather that he is trying to change the standard? He is merely
suggesting an extension. I quote: "and it is important to know that the C
standard doesn't forbid extensions".
I guess is more of a moral one (which may or may not be off topic).
In regards to the original question: I wouldn't use it because I prefer to
stick to the standard, but I find the concept very neat and I do think it
would be a useful extension which could clarify and simplify code.
Good luck,
--
Martijn Haak http://www.sereneconcepts.nl
"David Rubin" <bo***********@ nomail.com> wrote in message news:3F******** *******@nomail. com... jacob navia wrote:
[snip - I introduced operator overloading as an extension to my compiler suite] What do you think?
If you want to use C++ features, use C++.
/david
Obviously I do not agree with that. C++ is a very complex language, and is missing an essential
facility I found only in C: I like to build my structures/data as I want, without any object
oriented framework imposed by the language.
There are many things innovative in C++, but not all of it. This idea solves a problem that can't be
solved in C easily, without adding any C++ complexity.
jacob
"bd" <bd*****@bd-home-comp.no-ip.org> wrote in message
news:pa******** *************** *****@bd-home-comp.no-ip.org... On Fri, 08 Aug 2003 21:53:32 +0200, jacob navia wrote:
C++ introduced an interesting feature (among others): operator overloading.
The idea is to build a mechanism for the user defining its own number types and the operations
to be done with them.
I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce
350 bit floats.
[snip]
This is offtopic for comp.lang.c. comp.std.c is for discussion of changes to the standard.
I wouldn't say the standard should be changed, at least not before a good discussion. This is what
comp.lang.c is all about isn't it?
Discussion about C.
Operator overloading allows the easy introduction of new numeric types without much problems. I am
not saying is "the solution" for all problems but is "a solution" for the introduction of
1) Higher precision numbers
2) Bignums (arbitrary precision numbers)
3) Rationals (Numbers expressed as integer ratios 5/9, for instance)
4) Quaternions
and many others. To do this in C requires
divide(add(a,b) ,sub(a,b))
instead of
(a+b)/(a-b)
jacob
"jacob navia" <ja*********@ja cob.remcomp.fr> wrote in message Operator overloading allows the easy introduction of new numeric types without much problems
I wrote a big number package in C++. It had plenty of problems, for instance
I added a user defined cast to long to try to simplify the code, and
promptly broke everything I had written previously.
"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message news:bh******** **@news6.svr.po l.co.uk... "jacob navia" <ja*********@ja cob.remcomp.fr> wrote in message Operator overloading allows the easy introduction of new numeric types without much problems
I wrote a big number package in C++. It had plenty of problems, for instance I added a user defined cast to long to try to simplify the code, and promptly broke everything I had written previously.
Well, give lcc-win32 a try. I will be glad to help you if you find problems with it.
jacob
"Zeljko Vrba" <mo****@fly.srk .fer.hr> wrote in message news:sl******** ***********@fly .srk.fer.hr... In article <bh**********@n ews-reader5.wanadoo .fr>, jacob navia wrote: What do you think? I wouldn't use it because of: - it is supported only by your compiler; if I want to make my program available to general public (i.e. UNIX systems, non x86 systems), the feature is useless - I don't find prefix, function-call synax hard, and it's standard C - unclear semantics: given
BIGNUM operator+(BIGNU M, double)
and an expression like a+2 (where a is BIGNUM), will 2 get converted to double or an error will result?
If you compile this
#include <stdio.h>
typedef struct tagB {
int a;
int b;
} B;
B operator+(B a,double d)
{
printf("operato r + called\n");
return a;
}
int main(void)
{
B a,b;
a = a+2;
return 0;
}
This produces
"operator + called"
Will this allow me to define additional BIGNUM operator+(BIGNU M, int)
Yes. But in that case the int operator+ will be called. The algorithm first seeks a correct match
without any promotions. Then it will do "the usual conversions", then it will look if there are
defined casts that could match. I say C++ features are best left to C++.
I say operator overloading is useful.
"jacob navia" <ja*********@ja cob.remcomp.fr> writes: C++ introduced an interesting feature (among others): operator overloading.
<explanation snipped>
Just my 2c: operator (and function) overloading are about the only C++
features I miss since I switched (back) to C.
I think a number of replies to your post reflect the fear that, as
soon as one starts to introduce new features, it will be impossible to
stop and C will soon become as complex as C++. And I don't deny that
there may be some truth in this claim.
Good luck,
--
Stefano
In article <bh**********@n ews-reader3.wanadoo .fr>, jacob navia wrote: I say operator overloading is useful.
I didn't say it isn't. But then use C++. Nobody forces you to use classes,
inheritance, access levels and other C++ novelties. You can use it just as
an "enhanced C" using only features that you like/need/feel comfortable with.
Don't get me wrong: you did a tremendous amount of work coding a C compiler
and I admire that (having listened through 2 semesters of formal languages
and automata theory at faculty I know what is involved :)).
I just don't feel comfortable about spreading around another variant of C
with its own extensions that are incompatible with others in a fundamental
way (code written for your compiler would need a major revision if/when need
arises to port it to ANSI C).
Diversity of dialects killed LISP (the only other language I find beautiful
besides C) and Basic (at least until Gates "standardiz ed" it). We shouldn't
be doing it to C too. 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, but I would like to use
internally my own = operator for some of my value types, so I can say "x =
y;" rather than "x.Assign(y);".
The op_Assign operator seems impossibly broken since it takes __value copies
of the two objects. Perhaps there is...
|
by: Pmb |
last post by:
I've been working on creating a Complex class for my own learning purpose
(learn through doing etc.). I'm once again puzzled about something. I can't
figure out how to overload the assignment operator.
Here's what I'm trying to do. I've defined class Complex as
class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
|
by: gorda |
last post by:
Hello,
I am playing around with operator overloading and inheritence,
specifically overloading the + operator in the base class and its
derived class.
The structure is simple: the base class has two int memebers "dataA",
"dataB". The derived class has an additional int member "dataC". I am
simply trying to overload the + operator so that 'adding' two objects
will sum up the corresponding int members.
|
by: pmatos |
last post by:
Hi all,
I'm overloading operator<< for a lot of classes. The question is about
style. I define in each class header the prototype of the overloading
as a friend. Now, where should I define the overloading of operator<<.
In the .cc of the respective class or in a file where I am overloading
operator<< for all classes?
Cheers,
|
by: carlos |
last post by:
Curious:
Why wasnt a primitive exponentiation operator not added to C99?
And, are there requests to do so in the next std revision?
Justification for doing so:
C and C++ are increasingly used in low-level
numerical computations, replacing Fortran in newer projects.
Check, for example, sourceforge.net or freshmeat.net
But neither language offers a primitive exp operator.
| |
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: Jerry Fleming |
last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone
says that the four operators can only be overloaded with class member
functions instead of global (friend) functions: (), , ->, =. I wonder
why there is such a restriction.
Some tutorials say that 'new' and 'delete' can only be overloaded with
static member functions, others say that all overloading function should
be non-static. Then what is the fact, and why?
...
|
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, obj2 ;
|
by: sturlamolden |
last post by:
Python allows the binding behaviour to be defined for descriptors,
using the __set__ and __get__ methods. I think it would be a major
advantage if this could be generalized to any object, by allowing the
assignment operator (=) to be overloaded.
One particular use for this would be to implement "lazy evaluation".
For example it would allow us to get rid of all the temporary arrays
produced by NumPy.
For example, consider the...
|
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,
operator =, operator, operator(), and operator-must be nonstatic
member function; this ensures that their first operands will be
lvalues". I know that these operators must be nonstatic member
functions, but why this ensure their first operands will...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |