Re: Operator overloading in C
- From: Ian Collins <ian-news@xxxxxxxxxxx>
- Date: Sun, 09 Sep 2007 09:01:42 +1200
jacob navia wrote:
Ian Collins wrote:Which would be counterintuitive to most programmers.
jacob navia wrote:
To cope with the case where operator=() calls malloc?
I do not see why destructors are necessary.
In C it's the programmer's / library writer responsibility.
int fn(void)
{
qfloat m = StringToQfloat("12345674.55666655565665");
}
What can the user do if StringToQfloat calls malloc?
Operator overloading is the same:
int fn(void)
{
qfloat m = "12345674.55666655565665";
// ...
free(m);
}
Note: C is NOT C++. The operator overloading feature willIt isn't in C++ either. An operator is just a function with a shorthand
NOT be different than other function calls in C.
syntax. An operator in C++ can and sometimes has to be called by its
full name (on a pointer variable for example).
Correct documentation of your operator allows for using malloc,But you can still end up with temporary intermediate objects in a
but that will become difficult when we have:
qfloat m,n;
m = (m+n)/23;
That is why is much better to return whole objects instead
of pointers that are allocated with malloc. Instead of
qfloat *operator+(qfloat,qfloat);
You should rather use:
qfloat operator+(qfloat,qfloat);
non-trivial equation.
So you claim constructors and destructors are a more complex addition
The GC can be used to avoid this problem (as is used in lcc). But there
is NO easy solution.
To introduce destructors / constructors into C would be breaking the
simplicity of C, its main advantage.
than garbage collection?
They have done C++ a huge favor in saving that language form the horrors
of GC. Once a language feature requires GC, the language ceases to be
appropriate for embedded or systems programming.
--
Ian Collins.
.
- Follow-Ups:
- Re: Operator overloading in C
- From: jacob navia
- Re: Operator overloading in C
- References:
- Operator overloading in C
- From: jacob navia
- Re: Operator overloading in C
- From: jacob navia
- Re: Operator overloading in C
- From: Douglas A. Gwyn
- Re: Operator overloading in C
- From: Richard Heathfield
- Re: Operator overloading in C
- From: lawrence . jones
- Re: Operator overloading in C
- From: Douglas A. Gwyn
- Re: Operator overloading in C
- From: jacob navia
- Re: Operator overloading in C
- From: Douglas A. Gwyn
- Re: Operator overloading in C
- From: Douglas A. Gwyn
- Re: Operator overloading in C
- From: jacob navia
- Re: Operator overloading in C
- From: Ian Collins
- Re: Operator overloading in C
- From: jacob navia
- Operator overloading in C
- Prev by Date: Re: Operator overloading in C
- Next by Date: Re: Operator overloading in C
- Previous by thread: Re: Operator overloading in C
- Next by thread: Re: Operator overloading in C
- Index(es):
Relevant Pages
|