Re: Operator overloading in C



jacob navia wrote:
Ian Collins wrote:
jacob navia wrote:

I do not see why destructors are necessary.

To cope with the case where operator=() calls malloc?


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);
}

Which would be counterintuitive to most programmers.

Note: C is NOT C++. The operator overloading feature will
NOT be different than other function calls in C.

It isn't in C++ either. An operator is just a function with a shorthand
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 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);

But you can still end up with temporary intermediate objects in a
non-trivial equation.

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.

So you claim constructors and destructors are a more complex addition
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.
.



Relevant Pages

  • Re: Operator overloading in C
    ... int main ... I do not see why destructors are necessary. ... The operator overloading feature will ... qfloat *operator+; ...
    (comp.std.c)
  • Re: Destructor: not gauranteed to be called?
    ... > implementation detail for destructors in C++. ... Destruction is a language ... Resource automatic freeing (mainly memory), ... The problem is that most developper know about finalizers (which ...
    (microsoft.public.dotnet.languages.vc)
  • Re: a question on execution order
    ... since the result may be a local object, I'd expect local destructors ... struct tricky ... int& x; ... int f(int& outer) ...
    (microsoft.public.vc.language)
  • Re: On the development of C
    ... types will require constructors and destructors. ... advocating the addition only of qfloat, then I think we do not need ...
    (comp.lang.c)
  • Re: Tail Recursion, Stack and Function Call after "return"
    ... (actually it's a private case of Tail Call Optimization, TCO) ... It is because these destructors must run before ... int foo() ... be valid and occupy a storage on stack during execution of `bar'. ...
    (microsoft.public.vc.language)