Why do I have to declare my oper<< as friend in my class as follows:
class A {
public:
friend std::ostream& operator<<(std::ostream& out, const A& a)
{ return out << "foo"; }
}; 9 1668
vineoff ha scritto: Why do I have to declare my oper<< as friend in my class as follows:
class A { public: friend std::ostream& operator<<(std::ostream& 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::ostream& operator<<(std::ostream& out, const A& a)
{ return out << "foo"; }
vineoff wrote: Why do I have to declare my oper<< as friend in my class as follows:
class A { public: friend std::ostream& operator<<(std::ostream& out, const A& a) { return out << "foo"; } };
No reason, i.e. you don't have to.
But can you show me an example without friend keyword. If I just remove
it now, it won't compile
vineoff wrote: 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::ostream& operator<<(std::ostream& 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.
:)
vineoff wrote: 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::ostream& operator<<(std::ostream& out, const A& a)
{ return out << "foo"; }
int main()
{
}
Why operator== i.e. can be declared as member function but operator<<
can't?
vineoff wrote: Why operator== i.e. can be declared as member function but operator<< can't?
This is an FAQ: http://www.parashift.com/c++-faq-lit....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::ostream, since
the left-hand-side parameter is of type std::ostream 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::ostream
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::ostream on the right-hand-side when you used the operator, which
would look very strange.
Best regards,
Tom
vineoff wrote: Why operator== i.e. can be declared as member function but operator<< can't?
This is an FAQ: http://www.parashift.com/c++-faq-lit....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::ostream, since
the left-hand-side parameter is of type std::ostream 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::ostream
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::ostream on the right-hand-side when you used the operator, which
would look very strange.
Best regards,
Tom
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alicia |
last post by:
Hello,
I am trying to figure out how to call an overloaded operator<<
inherited from a base class.
#ifndef PHONECALL
#define PHONECALL
#include "time.h"
#include "interval.h"
|
by: Rock |
last post by:
I'm in the process of writing this program for complex numbers and I
use DevC++. My professor on the other hand compiles on Borland 5.5. So
I ocasionally save and run my work on Borland to see if...
|
by: Harry |
last post by:
Hi all,
I am writing a logger program which can take any datatype.
namespace recordLog {
enum Debug_Level {low, midium, high};
class L {
std::ofstream os;
Debug_Level cdl;
const...
|
by: Amadeus W. M. |
last post by:
What is the difference between
friend ostream & operator<<(ostream & OUT, const Foo & f){
// output f
return OUT;
}
and
template <class X>
|
by: freegnu |
last post by:
how to declare a friend function that can access two class
it will look like the following
class A
{
private:
int i;
public:
A(){}
~A(){}
friend void call(A &a, B &b);
|
by: noone |
last post by:
hi.
I don't use exceptions much in the embedded world, but for my plugin
interface to a hardware MPEG encoder I'd like to, since there are so many
places that the crummy kernel driver can do bad...
|
by: aaragon |
last post by:
Hi everyone,
I was unable to find out why my code is not compiling. I have a
template class and I'm trying to write the operator<< for standard
output. Does anyone know why this is not right?...
|
by: subramanian100in |
last post by:
Consider a class that has
vector< pair<int, string>* c;
as member data object.
I need to use operator>to store values into this container object
and
operator<< to print the contents of the...
|
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= |
last post by:
I'm having an error with this code
#include <iostream>
template < typename Tclass TestOpTemplate
{
public:
friend std::ostream& operator<< <>(std::ostream& os, const
TestOpTemplate<T>& m);...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |