POV-Ray : Newsgroups : povray.off-topic : C++ destructor question : Re: C++ destructor question Server Time
3 Sep 2024 21:17:29 EDT (-0400)
  Re: C++ destructor question  
From: Warp
Date: 6 Aug 2010 13:42:12
Message: <4c5c4974@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> >   You would have a pretty bad danger of memory leak if arrays wouldn't
> > destroy their elements.

> Yeah, I just wasn't thinking clearly, thinking "arrays aren't classes, so 
> they don't have destructors."

  Curiously, *all* primitive types in C++ have default constructors. At
least in syntax. (This default constructor initializes the variable to zero.)
It can be called like any other constructor. So you could have something
like:

    void foo(int i = int()) { ... }

  In other words, the foo() function takes optionally an int as parameter,
and if none is specified, it takes a default-initialized int instead.

  Of course the above is *completely* equivalent to:

    void foo(int i = 0) { ... }

so what's the point? It becomes important with (yeah, you guessed it)
templates. You can write something like:

    template<typename T>
    class MyClass
    {
     public:
        void foo(T value = T());
    };

and it will work even if T is a primitive type (such as int).

  (The default function parameter value is just one example of many
situations where it becomes useful.)

-- 
                                                          - Warp


Post a reply to this message

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