friend oper<<

V

vineoff

Why do I have to declare my oper<< as friend in my class as follows:

class A {
public:
friend std::eek:stream& operator<<(std::eek:stream& out, const A& a)
{ return out << "foo"; }
};
 
M

marco

vineoff ha scritto:
Why do I have to declare my oper<< as friend in my class as follows:

class A {
public:
friend std::eek:stream& operator<<(std::eek:stream& out, const A& a)
{ return out << "foo"; }
};
Why did you do? It is necessary only if you want to have access to
non-public members of class A within operator <<.

In this case you can write
class A {};
std::eek:stream& operator<<(std::eek:stream& out, const A& a)
{ return out << "foo"; }
 
R

Rolf Magnus

vineoff said:
Why do I have to declare my oper<< as friend in my class as follows:

class A {
public:
friend std::eek:stream& operator<<(std::eek:stream& out, const A& a)
{ return out << "foo"; }
};

No reason, i.e. you don't have to.
 
V

vineoff

But can you show me an example without friend keyword. If I just remove
it now, it won't compile
 
X

xian

vineoff said:
But can you show me an example without friend keyword. If I just remove
it now, it won't compile

like this :)

#include<iostream>

class A {
public:
int val;
};
std::eek:stream& operator<<(std::eek:stream& out, const A& a)
{
return out << a.val;
}

int main()
{
A a;
std::cout << a;
}

And Scott Meyers said: Don't let operator << or operator >> be the
members. And if you want to access the private members, let it be a
friend.

:)
 
R

Rolf Magnus

vineoff said:
But can you show me an example without friend keyword. If I just remove
it now, it won't compile

The operator must be a non-member, which it is automatically if you declare
it as friend. So you have to write:

class A {
public:
};

std::eek:stream& operator<<(std::eek:stream& out, const A& a)
{ return out << "foo"; }

int main()
{
}
 
T

Thomas Tutone

vineoff said:
Why operator== i.e. can be declared as member function but operator<<
can't?

This is an FAQ:

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.8

It's not that you can't declare operator<<() as a member function, it's
that it wouldn't do what you seem to think it would.

In short, to declare operator<<() as a member function, you would have
to modify the declaration and definition of class std::eek:stream, since
the left-hand-side parameter is of type std::eek:stream and the
right-hand-side parameter is the object you want to send to the
ostream. And of course, you are not permitted to modify std::eek:stream
yourself.

If you, on the other hand, made operator<<() a member of your class
Foo, then you would have to put Foo on the left-hand-side and the
std::eek:stream on the right-hand-side when you used the operator, which
would look very strange.

Best regards,

Tom
 
T

Thomas Tutone

vineoff said:
Why operator== i.e. can be declared as member function but operator<<
can't?

This is an FAQ:

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.8

It's not that you can't declare operator<<() as a member function, it's
that it wouldn't do what you seem to think it would.

In short, to declare operator<<() as a member function, you would have
to modify the declaration and definition of class std::eek:stream, since
the left-hand-side parameter is of type std::eek:stream and the
right-hand-side parameter is the object you want to send to the
ostream. And of course, you are not permitted to modify std::eek:stream
yourself.

If you, on the other hand, made operator<<() a member of your class
Foo, then you would have to put Foo on the left-hand-side and the
std::eek:stream on the right-hand-side when you used the operator, which
would look very strange.

Best regards,

Tom
 
T

Thomas Tutone

Thomas Tutone wrote:

[Quote of repetitive post snipped ...]

Well, I posted this ONCE at around 1:30pm New York time via Google
Groups, and it has appeared TWICE on usenet - once at 2:37pm, and again
at 3:01pm.

Goodness only knows what is going on at Google...

Best regards,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top