Connecting Tech Pros Worldwide Forums | Help | Site Map

Friends and Namespaces

Simon
Guest
 
Posts: n/a
#1: Jul 19 '05
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;
}



WW
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Friends and Namespaces


Simon wrote:[color=blue]
> 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.[/color]
[SNIP][color=blue]
> class Foo {
> public:
> Foo() { foo_int = 101; }
> private:
> int foo_int;
> friend Bar(const Foo& f);[/color]

friend mynamespace::Bar(const Foo& f);
[color=blue]
> };
>
> namespace mynamespace {
> int Bar(const Foo& f) {[/color]
[SNIP]

Bar is not called Bar anymore. It is mynamespace::Bar. With your original
libe you gave friendship to a function called Bar, in the global namespace.

--
WW aka Attila


tom_usenet
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Friends and Namespaces


On Mon, 6 Oct 2003 15:14:14 +0100, "Simon" <sorry@no.mail> wrote:
[color=blue]
>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.[/color]

You have to declare friendship to the function in the namespace, not
to a non-existent global one.
[color=blue]
>
>Thanks for your help,
>Simon ;o)
>
>//---------------
>
>#include <iostream>[/color]

class Foo;

namespace mynamespace {
int Bar(const Foo& f);
}
[color=blue]
>
>class Foo {
>public:
> Foo() { foo_int = 101; }
>private:
> int foo_int;
> friend Bar(const Foo& f);[/color]

friend mynamespace::Bar(const Foo& f);

[color=blue]
>};
>
>namespace mynamespace {
> int Bar(const Foo& f) {
> return f.foo_int;
> }
>}
>
>int main() {
> Foo f;
> std::cout << mynamespace::Bar(f);
> return 0;
>}[/color]

Tom
Jonathan Mcdougall
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Friends and Namespaces


> > I have a class Foo which defines a friend function Bar. When I place[color=blue][color=green]
> > 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.[/color][/color]
[color=blue][color=green]
> > class Foo {
> > public:
> > Foo() { foo_int = 101; }
> > private:
> > int foo_int;
> > friend Bar(const Foo& f);[/color]
>
> friend mynamespace::Bar(const Foo& f);[/color]

Just to be accurate, you'll get a bunch of errors since
1) mynamespace is undeclared at this point
2) the Bar declaration has no return value

class Foo;

namespace mynamespace
{
int Bar(const Foo& f);
}

class Foo
{
..
friend int mynamespace::Bar(const Foo &f);
};

...


Jonathan


Closed Thread