Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > friends namespaces and operators

Reply
Thread Tools

friends namespaces and operators

 
 
glen stark
Guest
Posts: n/a
 
      10-12-2004
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 the cpp code I implemented
the operator overloading within a "using namespace" context, only to get
access errers. To get it to work I had to put a namespaceName:: in
front of the operator keyword...

for your edification, here is a sample code:
================
HEADER FILE

#include <string>
#include <ostream>

namespace voxel{
class PrivateClass{
public:
PrivateClass():info("hiya"){};
friend std:stream& operator<<(std:stream&, const PrivateClass&);
private:
std::string info;
};
}
===================
CPP FILE

#include "stdafx.h"
#include "PrivateClass.h"
using namespace voxel;
std:stream& voxel:perator<<(std:stream& os, const PrivateClass& pc)
{
os<<pc.info;
return os;
}

int main(int argc, char* argv[])
{

PrivateClass pc;
std::cout<<pc;
return 0;

}


=====end of code

So my question is: is this a compiler bug, or is this a loophole in the
standard somehow? If it's a compiler bug, does the standard imply that
it should behave as i would think, or what? I would very much like any
clarification you can offer me.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-12-2004

"glen stark" <> wrote in message
news:416beaa3$...
> 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 the cpp code I implemented
> the operator overloading within a "using namespace" context, only to get
> access errers. To get it to work I had to put a namespaceName:: in
> front of the operator keyword...
>
> for your edification, here is a sample code:
> ================
> HEADER FILE
>
> #include <string>
> #include <ostream>
>
> namespace voxel{
> class PrivateClass{
> public:
> PrivateClass():info("hiya"){};
> friend std:stream& operator<<(std:stream&, const PrivateClass&);
> private:
> std::string info;
> };
> }
> ===================
> CPP FILE
>
> #include "stdafx.h"
> #include "PrivateClass.h"
> using namespace voxel;
> std:stream& voxel:perator<<(std:stream& os, const PrivateClass& pc)
> {
> os<<pc.info;
> return os;
> }
>
> int main(int argc, char* argv[])
> {
>
> PrivateClass pc;
> std::cout<<pc;
> return 0;
>
> }
>
>
> =====end of code
>
> So my question is: is this a compiler bug, or is this a loophole in the
> standard somehow?


Neither, its your understanding of C++ that is wrong.

> If it's a compiler bug, does the standard imply that
> it should behave as i would think, or what? I would very much like any
> clarification you can offer me.


You seem to be assuming that writing 'using namespace voxel;' should put
subsequent definitions in the voxel namespace but that is not the case (if
it were the case then you would have put main in the voxel namespace). Or
maybe you are thinking that the compiler should somehow make a connection
between the operator<< in your source file and the operator<< in your header
file, but as far as the compiler is concerned they are just similar
functions in different namespaces.

'Using namespace ...' affects how names are looked up, it does not affect
which namespace names are defined in. The only way to do that is this

namespace voxel
{
std:stream& operator<<(std:stream& os, const PrivateClass& pc)
{
...
}
}

or this

std:stream& voxel:perator<<(std:stream& os, const PrivateClass&
pc)
{
...
}

john


 
Reply With Quote
 
 
 
 
glen stark
Guest
Posts: n/a
 
      10-12-2004
John Harrison wrote:

> "glen stark" <> wrote in message
> news:416beaa3$...
>
>>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 the cpp code I implemented
>>the operator overloading within a "using namespace" context, only to get
>>access errers. To get it to work I had to put a namespaceName:: in
>>front of the operator keyword...
>>
>>for your edification, here is a sample code:
>>================
>>HEADER FILE
>>
>>#include <string>
>>#include <ostream>
>>
>>namespace voxel{
>>class PrivateClass{
>>public:
>> PrivateClass():info("hiya"){};
>> friend std:stream& operator<<(std:stream&, const PrivateClass&);
>>private:
>> std::string info;
>>};
>>}
>>===================
>>CPP FILE
>>
>>#include "stdafx.h"
>>#include "PrivateClass.h"
>>using namespace voxel;
>>std:stream& voxel:perator<<(std:stream& os, const PrivateClass& pc)
>>{
>> os<<pc.info;
>> return os;
>>}
>>
>>int main(int argc, char* argv[])
>>{
>>
>> PrivateClass pc;
>> std::cout<<pc;
>> return 0;
>>
>>}
>>
>>
>>=====end of code
>>
>>So my question is: is this a compiler bug, or is this a loophole in the
>>standard somehow?

>
>
> Neither, its your understanding of C++ that is wrong.
>
>
>>If it's a compiler bug, does the standard imply that
>>it should behave as i would think, or what? I would very much like any
>>clarification you can offer me.

>
>
> You seem to be assuming that writing 'using namespace voxel;' should put
> subsequent definitions in the voxel namespace but that is not the case (if
> it were the case then you would have put main in the voxel namespace). Or
> maybe you are thinking that the compiler should somehow make a connection
> between the operator<< in your source file and the operator<< in your header
> file, but as far as the compiler is concerned they are just similar
> functions in different namespaces.
>
> 'Using namespace ...' affects how names are looked up, it does not affect
> which namespace names are defined in. The only way to do that is this
>
> namespace voxel
> {
> std:stream& operator<<(std:stream& os, const PrivateClass& pc)
> {
> ...
> }
> }
>
> or this
>
> std:stream& voxel:perator<<(std:stream& os, const PrivateClass&
> pc)
> {
> ...
> }
>
> john
>
>

Thanks, that clears it up.

Glen
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hello friends...wanna make friends visit nicki C Programming 0 10-13-2008 11:51 AM
MEET UR SCHOOL & COLLEGE FRIENDS. UR FRIENDS ARE WAITING FOR U.. sai.sri206@gmail.com C++ 0 10-28-2007 08:43 PM
Friends don't let friends drink and fly through space =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-29-2007 03:52 AM
member functions as friends - friends of each other? bipod.rafique@gmail.com C++ 2 07-16-2005 10:55 AM
friends, namespaces and operators. glen stark C Programming 3 10-12-2004 07:08 PM



Advertisments