Connecting Tech Pros Worldwide Help | Site Map

Friends and Namespaces

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 06:55 PM
Simon
Guest
 
Posts: n/a
Default Friends and Namespaces

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;
}



  #2  
Old July 19th, 2005, 06:55 PM
WW
Guest
 
Posts: n/a
Default 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


  #3  
Old July 19th, 2005, 06:55 PM
tom_usenet
Guest
 
Posts: n/a
Default 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
  #4  
Old July 19th, 2005, 06:56 PM
Jonathan Mcdougall
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.