S
Simon
Hi,
I have a class Foo which defines a friend function Bar. When I place the Bar
function within a namespace, I can no longer access the Foo private data
(i'm guessing it is not recognised as a friend). Could someone explain this
behaviour, as i'm obviously not understanding namespaces properly.
Thanks for your help,
Simon ;o)
//---------------
#include <iostream>
class Foo {
public:
Foo() { foo_int = 101; }
private:
int foo_int;
friend Bar(const Foo& f);
};
namespace mynamespace {
int Bar(const Foo& f) {
return f.foo_int;
}
}
int main() {
Foo f;
std::cout << mynamespace::Bar(f);
return 0;
}
I have a class Foo which defines a friend function Bar. When I place the Bar
function within a namespace, I can no longer access the Foo private data
(i'm guessing it is not recognised as a friend). Could someone explain this
behaviour, as i'm obviously not understanding namespaces properly.
Thanks for your help,
Simon ;o)
//---------------
#include <iostream>
class Foo {
public:
Foo() { foo_int = 101; }
private:
int foo_int;
friend Bar(const Foo& f);
};
namespace mynamespace {
int Bar(const Foo& f) {
return f.foo_int;
}
}
int main() {
Foo f;
std::cout << mynamespace::Bar(f);
return 0;
}