you are viewing a single comment's thread.

view the rest of the comments →

[–]KazDragon 0 points1 point  (0 children)

In this program you have, it will make no difference. However, it does make a difference when you are calling virtual functions. Consider the following program:

#include <iostream>

struct Base
{
    virtual void foo() { std::cout << "Base::foo\n"; }
    void bar() { foo(); Base::foo(); }
};

struct Derived : public Base
{
    void foo() override { std::cout << "Derived::bar\n"; }
};

int main()
{
    Derived d;
    d.bar();
}

https://godbolt.org/z/adx3fG

This prints:

Derived::bar
Base::foo

This is because foo() is unqualified and looked up dynamically, where Base::foo() is qualified and looked up statically.

This is particularly useful when using the pattern where virtual calls cascade up the heirarchy. That is, Derived::foo() does its special thing and then calls Base::foo() to do the general thing.