Never Change the Default Parameters of Virtual Methods

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{  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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of