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 1690
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<iostre am>
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 it caught
anything, it's very picky... Anyway, the code below works on Dev, and
it compiles fine on Borland, but when I run it from borland, there is
no output, no error, it just skips right over the freind ostream call.
HELP! I'm new to this...
|
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 Debug_Level ddl;
|
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 things to my userland
program. I hope this news client doesn't reformat my example below and
make it impossible to read. :^)
consider the following exception heirarchy
|
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? The code is as
follows...
// main class:
template <
class Individual,
|
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 container.
I have written both these operators as non-friend functions.
|
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: 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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |