Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Operator overloading in C

Reply
Thread Tools

Operator overloading in C

 
 
jacob navia
Guest
Posts: n/a
 
      08-08-2003
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


 
Reply With Quote
 
 
 
 
bd
Guest
Posts: n/a
 
      08-08-2003
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

 
Reply With Quote
 
 
 
 
Martijn
Guest
Posts: n/a
 
      08-08-2003
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


 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      08-08-2003

"David Rubin" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      08-08-2003

"bd" <(E-Mail Removed)-ip.org> wrote in message
news(E-Mail Removed)-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


 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      08-09-2003

"jacob navia" <(E-Mail Removed)> 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.



 
Reply With Quote
 
Stefano Ghirlanda
Guest
Posts: n/a
 
      08-09-2003
"jacob navia" <(E-Mail Removed)> 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
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      08-09-2003

"Malcolm" <(E-Mail Removed)> wrote in message news:bh2kjr$2fd$(E-Mail Removed)...
>
> "jacob navia" <(E-Mail Removed)> 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


 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      08-09-2003

"Zeljko Vrba" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> In article <bh0vl6$nfk$(E-Mail Removed)>, 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+(BIGNUM, 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("operator + 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+(BIGNUM, 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.


 
Reply With Quote
 
Zeljko Vrba
Guest
Posts: n/a
 
      08-09-2003
In article <bh2ohn$a1e$(E-Mail Removed)>, 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 "standardized" it). We shouldn't
be doing it to C too.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments