Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Advanced
Feb 8, 2006



Application Security Testing: An Integral Part of DevOps

Never Change the Default Parameters of Virtual Methods

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
    virtual void foo(int i=0){ printf("%d",i); }

class Derived: public Base
    virtual void foo(int i=10){ printf("%d",i);}

  Base *pBase = new Derived();

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.

C.V. Ram Narayanan Sastry
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date