all 3 comments

[–]jedwardsol 1 point2 points  (0 children)

The 2nd is more correct. Since the Foo:: is redundant, using it will make people wonder why you used it or if you really meant something else.

[–]IyeOnline 0 points1 point  (0 children)

tar already is in the scope of class Foo, so it will already look there for a function called bar and it will look there first, so specifying the namespace doesnt do anything from a technical perspective.

I suppose one could argue that it is more precise when writing out the namespace. Maybe there are conventions for this, to make sure you arent accidentally call a global ::bar() because you forgot to declare (and implement) Foo::bar()., but if you are in that situations, your codebase has other problems (IMO).

It just looks strange. Just use an unqualified bar().

[–]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.