POV-Ray : Newsgroups : povray.off-topic : Coding in ___ is like ___ : Re: Coding in ___ is like ___ Server Time
4 Sep 2024 21:23:19 EDT (-0400)
  Re: Coding in ___ is like ___  
From: Warp
Date: 26 Feb 2010 13:59:50
Message: <4b881a25@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> >   Why would a beginner need to deal with constructors, destructors,
> > exceptions and the like?

> Because they'll be using libraries that require such.  Even in your example, 
> you'd have to talk about the std::string constructor and how it creates an 
> empty string if you don't pass it arguments.

  You don't have to deal with constructors. You can simply say "if you create
a string without initializing it, it defaults to an empty string".

  As opposed to char*, which defaults to... what?

> As opposed to (say) integers, that don't get initialized to zero.

  Well, in C++ they decided that if you don't initialize an integer, its
value is not guaranteed. Bummer, but nothing to do with constructors.

> And what happens if you run out of 
> memory in your concatenate function.

  Your program will be ended because it ran out of memory.

> >   No, C is a lot more complicated for the beginner precisely because it's
> > less powerful.

> I disagree. It's more complicated to get things done, but easier to understand.

  You seriously claim that eg. pointer arithmetic, null-terminated strings
and the standard C string manipulation functions are easier for a beginner
to understand than C++'s std::string? (And that's just one example.)

> It would be easier to explain what's happening.

  Does the beginner really need to know what the compiler is doing under
the hood in this case?

> I don't have to explain the syntax for method invocation

  There was no method invocation in my example. (Ok, operator+() *is* a
method, and it's being called, but the beginner doesn't need to know that
in order to use string concatenation.)

> const, references

  The C version will also have a const (the function should take a const
pointer) if it's well written. So it's not all that different.

  If you really want to avoid the reference in the C++ version, you can:

    std::string concatenate(std::vector<string> strings)
    {
        ...
    }

  It will still work (although will probably be less efficient). No such
luck in C, though. You can't pass an array of strings by value. You'll
have to have a pointer (a const one, preferably).

> the difference between references and pointers

  You don't need to know the difference between references and pointers in
order to implement the "concatenate" function in my example. (And as
mentioned, you don't *necessarily* even need references at all if you
really want to avoid them.)

> the differences between method invocations on 
> references and pointers

  My example didn't need any such thing.

> overloaded operators

  The beginner doesn't need to know that to implement that example.

> or what namespaces are.

  Is it really a bad thing that the beginner is introduced to namespaces
from the very beginning? It's not like they are very hard to understand.

> The 
> function will be longer and more dangerous, but the only "unusual" things to 
> be explained are pointers and strings.

  No. The C version would require explaining lots of things about how C
null-terminated strings work internally (which you don't have to with
std::string), how you allocate space for them in your code (which you don't
have to with std::string), how you make sure you have allocated enough space
for the result (again, no need with std::string) and how you concatenate
the strings with strcat (the only thing more or less equivalent to the
+ operator of std::string).

  You would also need to explain that if the function returns a pointer to
a new C string it allocated itself, how the calling code needs to make sure
that it's not leaked. (Again, no such need with std::string.)

  So the C++ version is enormously simpler than the C version in all possible
regards. The newbie needs to learn a lot less and can start doing some
practical (and safer) code much more quickly.

> Basically, in the C equivalent, the 
> only *confusing* part is that C doesn't allocate multi-word objects for you.

  No, it isn't. You have to understand null-terminated strings, how to
calculate the length of strings, and how to manually manage the memory
for the strings before you could write such a function in C.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.