473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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;
}
Jul 19 '05 #1
3 5585
WW
Simon wrote:
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. [SNIP] class Foo {
public:
Foo() { foo_int = 101; }
private:
int foo_int;
friend Bar(const Foo& f);
friend mynamespace::Bar(const Foo& f);
};

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

[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
Jul 19 '05 #2
On Mon, 6 Oct 2003 15:14:14 +0100, "Simon" <so***@no.mail> wrote:
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.
You have to declare friendship to the function in the namespace, not
to a non-existent global one.

Thanks for your help,
Simon ;o)

//---------------

#include <iostream>
class Foo;

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

class Foo {
public:
Foo() { foo_int = 101; }
private:
int foo_int;
friend Bar(const Foo& f);
friend mynamespace::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;
}


Tom
Jul 19 '05 #3
> > 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. class Foo {
public:
Foo() { foo_int = 101; }
private:
int foo_int;
friend Bar(const Foo& f);


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


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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
24
by: Marcin Vorbrodt | last post by:
Here is an example of my code: //Header file #include <vector> using std::vector; namespace Revelation { // class definitions, etc... // class members are of type std::vector }
2
by: glen stark | last post by:
Hi. I had an interesting experience with a brand x compiler recently. I had class defined within a namespace, and in that class I declared the "<<" operator as a friend to that class. Then, in...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.