|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> 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
|
![](/i/fill.gif) |