c++ - Polymorphism & default values: can co-exist? -


this question has answer here:

i have base class number of inherited derived classes. this:

class { public:     virtual void f(string foo = "bar") {         cout << foo << endl;     } };  class b: public { public:     void f(string foo = "howdy") {         cout << foo << endl;     } };  class c: public { public:     void f(string foo = "something") {         cout << foo << endl;     } }; 

i inherited 2 classes brevity.
main:

a* aarray[] = {     new b,     new c, };  int main() {     aarray[0]->f();     aarray[0]->f();      return 0; } 

when run program, output is:

bar bar 

just how if compiler ignores default arguments of overridden functions.
normal, or there i'm doing wrong or i'm missing?

default values statically binded. in other words, not have polymorphhic behavior. that's why saw

 bar  bar 

instead of default values in derived classes' virtual functions.

according effective c+:

if default parameter values dynamically bound, compilers have come way determine appropriate default values parameters of virtual functions @ runtime, slower , more complicated current mechanism of determining them during compilation.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -