POV-Ray : Newsgroups : povray.off-topic : C# 4.0 Default parameters : Re: C# 4.0 Default parameters Server Time
9 Oct 2024 16:15:01 EDT (-0400)
  Re: C# 4.0 Default parameters  
From: Warp
Date: 4 Feb 2009 12:04:43
Message: <4989caab@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> There are some who make good arguments against default parameters. 
> Basically, if it's a default parameter, it should be a separate method call 
> to set the value, because that's more OO.

  In an OO language where constructing an object also initializes it (so that
it's impossible to have uninitialized objects, which makes sense), it may be
rational to have more than one constructor for constructing objects in
different ways. It may also be rational to have a default-constructed object,
with some sensible default values. (And in some cases being able to
default-construct objects might be a prerequisite.)

  For example if you have a class named Point, which represents a
2-dimensional point with integer coordinates, it may be very rational
to have two constructors: A default constructor which initializes the
coordinates to zero, and another constructor which takes the values as
parameters. If we expressed that in C++, it would be:

class Point
{
 public:
    Point(); // Default constructor, initializes to (0,0)
    Point(int x, int y); // Initializes to the given values
};

  On the other hand, there isn't too much difference between that and:

class Point
{
 public:
    Point(int x = 0, int y = 0);
};

  Now the one and same constructor works both as the default constructor
and a constructor taking initial values.

  There may be one relevant difference from the point of view of the
implementation: Having only one constructor avoids code repetition,
especially if the constructor implementation is complicated.

  (OTOH, with the upcoming C++ standard it will be possible to call one
constructor from another, but until then...)

  One could argue: Why not have only the default constructor, and a
"set(int x, int y)" member function for setting the values?

  It becomes cumbersome when you need only a temporary, for example like:

    Point tmp;
    tmp.set(1, 2);
    foo(tmp);

rather than:

    foo(Point(1, 2));

  The former may also be harder for the compiler to optimize. The former
also makes it a lot more difficult to use move semantics (which might
become relevant if copying the objects is heavy).

  "Then don't use any default constructor at all, only the constructor
taking the parameters."

  However, then your class becomes less usable in situations like:

    std::vector<Point> vec;
    vec.resize(10); // Requires that Point has a default constructor

  Yes it's possible to give an object to the resize() function to initialize
the new objects from, but there may be situations where you just can't have
such a non-default-initialized object, such as:

    template<typename T>
    void foo()
    {
        std::vector<T> vec;
        vec.resize(10); // How would you know how to initialize an object
                        // of type T appropriately here?
    }

  Suppose the function is then called like: foo<Point>();

-- 
                                                          - Warp


Post a reply to this message

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