I have some (real world) code that has a structure basically like so:
class C
{
public:
C(int x = 0) : m_x(x) {}
friend int foo(const C& c, int y) { return c.m_x + y; }
private:
int m_x;
};
class Z
{
public:
int foo(int y) {
// The problematic call:
return foo(c, y);
}
private:
C c;
};
I understand that the call to foo
inside Z::foo
needs to be disambiguated. The thing I don't understand is, what scope is appropriate? I expected the declaration of the C
-friend foo
to be in the global namespace, but calling as ::foo
does not work, both GCC and Clang report this as an error.
However, if I change the declaration of C
+friend to:
class C
{
public:
C(int x) : m_x(x) {}
friend int foo(const C& c, int y);
private:
int m_x;
};
int foo(const C& c, int y) { return c.m_x + y; }
Everything works, I can (in Z::foo
) refer to ::foo
.
So here is my questions: in the first example, what scope is the C
-friend foo
in, if it is not in the global namespace? Is there no way to refer to the global foo
from within Z::foo
? And, what is the difference between my first and second examples?