It’s not a good idea to change the default parameters of virtual methods because the default parameters are bound at compile time (static binding), whereas virtual methods themselves get bound only at run time (late binding). Here’s an example:
class Base{ public: virtual void foo(int i=0){ printf("%d",i); }}class Derived: public Base{ public: virtual void foo(int i=10){ printf("%d",i);}}main(){ Base *pBase = new Derived(); pBase->foo();}
The expected output, based on the rules of virtual methods would be 10, because the Base pointer is pointing to the Derived object. However, because the default parameters are bound at compile time, they are bound assuming that pBase is a pointer to Base?and not to the Derived object, as is the case. Hence, the output is 0.
Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.























