POV-Ray : Newsgroups : povray.off-topic : C++ questions : Re: C++ questions Server Time
7 Sep 2024 07:21:50 EDT (-0400)
  Re: C++ questions  
From: Warp
Date: 25 Sep 2008 18:33:04
Message: <48dc11a0@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Correct. I was also pointing out that they get initialized to the type's 
> version of zero, whatever that might be. (I.e., pointers may not have 
> all zero bits in them; they get initialized to null instead.) Rarely can 
> you see this difference. :-)

  Actually it's possible to invoke the "default initialization" for any
type at any point in C++. For example if you do this:

    int i = int();

the 'i' variable will be default-initialized with the "zero" value for
the type in question (which, in this case, is the integer 0).

  One might ask what's the point. It's actually useful when you don't
know what the type actually is. This happens eg. with templates. You
could have, for example, this:

template<typename Type>
void foo()
{
    Type i = Type(); // Default-initialize i, even if it's a basic type
    ...
}

  Although mostly it's useful in situations where you want to create a
default-initialized temporary, for example in code like this:

    someVector.push_back(Type());

  That will work even if 'Type' is an int, a double or a pointer.

-- 
                                                          - Warp


Post a reply to this message

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