Original Post
I've stumbled across a minor oddity in the C++ language (or maybe it's just the MSVC 2003 implementation) yesterday. Here's an example...
#include <iostream>
#include <memory>
struct foo{
virtual void func( char* str = "foo::func" ){
std::cout << "entering: foo::func" << std::endl;
std::cout << str << std::endl;
}
};
struct bar : foo{
virtual void func( char* str = "bar::func" ){
std::cout << "entering: bar::func" << std::endl;
std::cout << str << std::endl;
}
};
int main(int,char**){
bar baz;
baz.func( );
std::auto_ptr<foo> p( new bar );
p->func( );
return 0;
}
--Outputs--
entering bar::func
bar::func
entering bar::func
foo::func
So what I don't understand is why virtual functions determine properly which function to call - but don't determine the correct default parameters. If I declared p as a pointer to bar then it would grab the proper default parameters. It just seems to go against the whole point of virtual functions to grab the default params based on what the pointer is declared as - not on the true polymorphic type of it. Could anyone shed some light on this for me - perhaps some kind of protection this yields or something else I could be overlooking? Thanks