Topic: Q: changing default arg of derived vfn
Author: reycri@atlantis.actrix.gen.nz (Reynaldo Crisostomo)
Date: 1995/04/13 Raw View
In article <3mhbjn$svs@engnews2.eng.sun.com>,
Steve Clamage <clamage@Eng.Sun.COM> wrote:
> In article 95Apr12122702@squirrel.jpl.nasa.gov, maxwell@squirrel.jpl.nasa.gov (Scott Maxwell) writes:
> >
> >If class Derived changes the value of a default argument of a virtual
> >member function, what value should that argument have when called
> >through a pointer (or reference) to a Base?
>
> If you invoke a function via a pointer to Base, the only things which
> are visible are the declarations in Base. Thus, you always get the Base
> version of the default argument values when you call a function via a
> Base pointer, reference, or object.
>
> A default argument value applies to the function declaration, not to the
> function definition. When you omit optional arguments to a function, the
> compiler supplies the default values at the call site. The function itself
> has no way of knowing what the source code looked like where it was called.
> It always gets the expected number of parameters.
>
> This is a good argument for not using different default argument values
> on overridden virtual functions, IMHO. Use function overloading instead.
>
> ---
> Steve Clamage, stephen.clamage@eng.sun.com
>
>
Let me just add that the default arguments are supplied at COMPILE-time.
This is in contrast to the RUN-time binding of virtual functions.
Hope this helps.
Rey Crisostomo
Wellington, New Zealand
- Speaking for myself.
Author: maxwell@squirrel.jpl.nasa.gov (Scott Maxwell)
Date: 1995/04/12 Raw View
If class Derived changes the value of a default argument of a virtual
member function, what value should that argument have when called
through a pointer (or reference) to a Base?
Here's some code illustrating what I mean:
#include <iostream.h>
class Base
{
public:
virtual void vfn(char * arg = "Base")
{
cout << "Base vfn called with arg == " << arg << endl;
}
};
class Derived: public Base
{
public:
virtual void vfn(char * arg = "Derived")
{
cout << "Derived vfn called with arg == " << arg << endl;
}
};
int main(void)
{
{
Base * b = new Base;
Base * d = new Derived;
b->vfn(); // I expect "arg == Base"
d->vfn(); // I expect "arg == Derived"
delete d; d = 0;
delete b; b = 0;
}
{
Base b;
Derived d;
b.vfn(); // I expect "arg == Base"
d.vfn(); // I expect "arg == Derived"
}
return 0;
}
g++ 2.6.3 says this:
Base vfn called with arg == Base
Derived vfn called with arg == Base
Base vfn called with arg == Base
Derived vfn called with arg == Derived
Is that correct (or at least compliant), or should the first call to
Derived::vfn have shown "arg == Derived"?
--
-------------------------+------------------------------------------------
// Scott Maxwell: | ``Beware, young Skywalker, the [C] preprocessor
\\// maxwell@ | comes from the Dark Side of the Force.''
XX natasha.jpl.nasa.gov | -- Kevlin A. P. Henney
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/04/12 Raw View
In article 95Apr12122702@squirrel.jpl.nasa.gov, maxwell@squirrel.jpl.nasa.gov (Scott Maxwell) writes:
>
>If class Derived changes the value of a default argument of a virtual
>member function, what value should that argument have when called
>through a pointer (or reference) to a Base?
If you invoke a function via a pointer to Base, the only things which
are visible are the declarations in Base. Thus, you always get the Base
version of the default argument values when you call a function via a
Base pointer, reference, or object.
A default argument value applies to the function declaration, not to the
function definition. When you omit optional arguments to a function, the
compiler supplies the default values at the call site. The function itself
has no way of knowing what the source code looked like where it was called.
It always gets the expected number of parameters.
This is a good argument for not using different default argument values
on overridden virtual functions, IMHO. Use function overloading instead.
---
Steve Clamage, stephen.clamage@eng.sun.com