POV-Ray : Newsgroups : povray.off-topic : Job Interview : Re: Job Interview Server Time
29 Jul 2024 12:21:41 EDT (-0400)
  Re: Job Interview  
From: Warp
Date: 5 Mar 2012 15:05:10
Message: <4f551c75@news.povray.org>
nemesis <nam### [at] gmailcom> wrote:
> BTW, why

>       Array(std::size_t size):
>           mData(new Value_t[size]),
>           mSize(size)
>       {}

> rather than

>       Array(std::size_t size)
>       {
>           mData=new Value_t[size];
>           mSize=size;
>       }

> when you've got exactly 2 extra chars (:,) in the first case?  Don't 
> tell me it's because some compile-time initialization that is not done 
> in the second case...

  Using the initialization list is recommended and, in some cases,
actually the only possible way of initializing certain members.
Usually you write actual code in the constructor only if a member
cannot be constructed in the initialization list. (In this particular
example there's no discernible benefit, except perhaps for a consistent
style of initialization.)

  Examples of members that can only be initialized in the initialization
list are const members, references and objects with no default constructor.
Also, even if the object had a default constructor but you wanted to
initialize with a different constructor, the only way to do that is to
do it in the initialization list.

//-------------------------------------------------------------------
class ClassWithoutDefaultConstructor
{
 public:
    ClassWithoutDefaultConstructor(int);
};

class ClassWithAdditionalConstructors
{
 public:
    ClassWithAdditionalConstructors();
    ClassWithAdditionalConstructors(int);
};

class Example
{
    int& referenceMember;
    const std::string constMember;
    ClassWithoutDefaultConstructor obj1;
    ClassWithAdditionalConstructors obj2;

 public:
    Example(int& aReference):
        referenceMember(aReference),
        constMember("Hello"),
        obj1(5),
        obj2(10)
    {
        // none of the above can be initialized here
    }
};
//-------------------------------------------------------------------

> is also delete[] defined when you define those operator[] methods?

  operator[] has nothing to do with new[] and delete[].

-- 
                                                          - Warp


Post a reply to this message

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