Good points, Keith.

These are arguments against misuse, though, not against using
object-oriented techniques. It's true that C++ provides some awfully
seductive features. Returning objects and passing objects by value is
tempting, until you try to do copy-construct large objects twice.
Overloading operators is tempting, but a class' clients can be misled by
the simple appearance. If your class provides complex features in a simple
way, people are likely to abuse them.

This is the programmer's old saw: a short program isn't necessarily faster.
How many times have you seen people justify

    ( x ? a : b )

by claiming it's faster or "tighter" code than

    if (x)
    {
      a
    }
    else
    {
      b
    }

..?

Well-coded C++ can be very nearly as efficient as C. For most good
programmers, I believe it's worth it. With some notable exceptions
(operating systems among them), the costs and risks of developing a complex
application far exceed the benefits of the tiny performance gains you
sacrifice when you go to C++.

I agree with you, the String example may be poorly chosen.

>And the disadvantages are not so obvious. The example you gave is not very
>different from:
>
>static void doSomething (char* final)
>{
>     char myString[20], aux[10];
>     strcpy (aux, "Testing");
>     strcpy (mystring, " string classes");
>     strcpy (final, aux);
>     strcat (final, mystring);
>}
>
>However, your version has a lot of hidden inefficiencies. First three string
>constructors are called. Second the initial states of those strings --
>carefully
>and lovingly set up in their constructors -- is immediately wiped out with the
>three subsequent assignment. Third, there is a temporary String created
>just to
>hold the intermediate result of "aux + mystring". Fourth, there is the
>assignment of "final" to the String object receiving the result of
>doSomething(). Fifth, four or five String objects have to be deleted. Finally,
>there are all of the hidden memory allocations associated with creating and
>manipulating several String objects.
>
>Perhaps one might scoff at these costs, saying that they are small compared to
>the benefits of being able to use the + operator on strings. However:
>
>* The Palm OS environment is very small. There's not much memory, and there's
>not much processing power. You can't afford to be extravagent to any degree.
>
>* I actually have some relevent experience in this area. I worked on the Pink
>project at Taligent for several years. For those who don't remember, this
>was a
>project to create a totally new, object-oriented operating system written in
>C++. It never made it to market. One of the reasons was precisely because of
>these hidden costs. It was way too easy to overcome the OS and the
>hardware. We
>were easily crippling the biggest, baddest, fastest, most expensive desktop
>hardware available at the time. And the reason for that was that the
>seductiveness of C++ allowed us to fall into the trap of writing bloated
>software. Looking at the code, it looked just fine, just like "string1 +
>string2" looks fine. But under the surface, a LOT is going on.
>
>Anyway, that's just my opinion, I could be wrong.
>
>-- Keith
>
>
>
>
>
>
>"Martin Krohn" <[EMAIL PROTECTED]> on 09/23/99 06:47:35 AM
>
>Please respond to [EMAIL PROTECTED]
>
>Sent by:  "Martin Krohn" <[EMAIL PROTECTED]>
>
>
>To:   [EMAIL PROTECTED]
>cc:    (Keith Rollin/HQ/3Com)
>Subject:  Re: C++ SDK wanted!
>
>
>
>
>Benefits of C++:
>Imagine you can write this:
>
>#include <StringLibrary.hpp>
>
>static String doSomething()
>{
>String mystring, aux, final;
>int i;
>    aux = "Testing ";
>    mystring = " string classes";
>    final = aux + mystring;
>    return final;
>}
>
>This is posible by writing a String class and overloading the "+" operator.
>Benefits are obvious.



Reply via email to