POV-Ray : Newsgroups : povray.off-topic : My first C++ program : Re: My first C++ program Server Time
1 Oct 2024 07:21:32 EDT (-0400)
  Re: My first C++ program  
From: Warp
Date: 21 Sep 2008 07:53:44
Message: <48d635c6@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> What is the "usual" C++ style for this kind of thing? (I'm asking a 
> question of style here; clearly there are several possible things you 
> *could* do in C++.)

  The standard data containers, as well as 'new', throw an exception by
default when they run out of memory. Also the at() member function of
some of the containers throws an exception if you try to index out of
boundaries.

  If you don't want to throw exceptions in your own code, then it depends
on what is it that you want to do to signal a condition.

  The usual way to catch programming errors at runtime is to use
assertions. For example:

void foo(int min, int max)
{
    assert(min <= max);

    // rest of the code here
}

  If the assertion fails, a message will be printed and the program is
immediately terminated at that point. Obviously this should only be
used in situations which are a symptom of a programming error and which
must be fixed.

  (The good thing about assert() is that you can freely use it as much
as you want. In fact, the more you use it, the better. You can compile
the program with a special precompiler parameter which will remove all
the asserts from the executable so that they will not add overhead to
a final version.)

-- 
                                                          - Warp


Post a reply to this message

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