Is there a context where the expression `a.b::c` makes sense?

A very simple example is if you want to call a member function of a parent class:

struct A {
    void f();
};

struct B: A {
    void f();
};

B b;
b.A::f();

One use case is accessing members of an enum within some struct A by using an instance of A (rather than using the enum directly via A::b::c):

struct A {
    enum class b { c }; // can be unscoped as well
};

A a;
a.b::c; // Access to enum value c - similarly, A::b::c would work

Here's a trivial example:

struct A {
    void f() {}
};

int main()
{
    A a;
    a.A::f();
}

A::f() is a qualified version of the name for the function f that's a member of A. You can use it in member access just like the "short" (or unqualified) name.

In fact, one might argue that every time you write a.f(), that's a shortcut for a.A::f() (with the A:: part being taken automatically from decltype(a)).

There's nothing magic about this, though it's unusual to see the construct outside of the sort of scenarios the other answerers demonstrated, because in this example it's redundant.

Tags:

C++