Ron Natalie said:
Vikram wrote:
Doesn't output anything, the program is ill-formed.
main must return int.
Defalut args is a compile-time thing. The call here is to
Base::NeedNothing, so the default arg is 10. The subsitution
of the overriding virtual function happens at the last point
of the calling procedure (after the name is looked up, overloading
is considered, access control is checked, and the arguments are
evaluated).
That's very strange!
I don't dispute it, but it's certainly not
intuitive. Suppose one wanted polymorphic behavior regarding the
parameter...how would one accomplish having a different default parameter in
the two functions (base and derived versions)? I don't know when I would
need it, but it seems like something that would be useful, somewhere.
One solution would be to have a member variable that is set in the
constructors, and use that whenever some "special" value is detected as the
parameter (and make that special value the default value for the parameter).
But that requires having such a "special" value available, and such may not
be the case.
Another alternative might be to forego the default parameter value, and
instead have a second (virtual) function with no parameter, whose job is to
call the first function with the parameter value that the specific class
requires.
That second one sounds better to me, unless you're worried about the
function call overhead I suppose. Thoughts?
-Howard