Re: Problem with linker
On Wed, 13 Jun 2007 07:56:07 GMT, MrAsm <mrasm@usa.com> wrote:
I still don't quite understand why Joe is against about default
arguments only in ctors; I think that his examples were very "ad hoc",
very specific. If code is designed well IMHO default arguments can be
good also in ctors...
I've already addressed this in several replies to Joe, but to be succinct
and blunt, his examples were based on a flawed understanding of how default
arguments and overloading work. Maybe he once encountered a compiler bug,
maybe not, but things were _never_ *supposed* to work the way he described.
So my advice is to focus on what I said in my "summary" message, and when
you use default arguments, always consider the equivalent set of overloaded
functions you are in reality defining. It's particularly important to do
the latter when you add a new overload to the fray, and Joe was absolutely
right about that.
If you're just aching for a genuine reason to avoid default arguments,
don't use them with virtual functions. Why? There's nothing to require
derived classes to provide the same default arguments. For example,
consider:
struct B
{
virtual void f(int x = 0);
};
struct D : B
{
void f(int x = 2);
};
D d;
B& b = d;
b.f();
You might think that since D::f is the function actually called, its x
parameter will be set to 2, but you'd be wrong. It'll be set to 0, because
that is the value in B::f, B is the static type of the object used to call
f, and it is the static type of the object that determines default argument
values. Thus, the standard advice is to declare D::f to respect B::f as
D::f(int x = 0), but the better rule of thumb is to avoid default arguments
in virtual functions.
--
Doug Harrison
Visual C++ MVP