what will happen, if we forget to return ostream referance ? -
class A
-
{
-
-
Private:
-
int _a;
-
int _b;
-
-
public:
-
-
A( int i, int j) : a(i), b(j)
-
{}
-
-
friend ostream& operator<<(ostream &os, const A &obj)
-
{
-
os << obj._a << obj._b << endl;
-
return os; // if i forget to return this referance.....
-
}
-
};
-
-
int main()
-
{
-
-
A aobj = new A(5,10);
-
-
cout<< aobj;
-
-
return 0;
-
}
-
O/P = 5 10 //Answer
OR
can we use like this -
friend void operator<<(ostream &os, const A &obj)
-
{
-
os << obj._a << obj._b << endl;
-
-
}
-
will the ouput will be the same.....
3 1717
You will return a copy of the ostream. That means your position pointers are lost. Anticipate screwed up displays.
The return of a reference is to avoid this copy so you can code:
cout << a << b << c;
Every action in C++ has a result and a side-effect. The result of cout << 5; is to return the ostream variable (cout). The side-effect is to put 5 into the ostream. Returning the ostream variable permits chaining multiple similar commands together. Without chaining, 3+4+5 is not possible; neither is cout << 3 << 4 << 5; If you return the ostream variable, the compiler takes several steps, first executing cout << 3; When cout is returned, it then has the code cout << 4 << 5; It continues this way until it outputs (or adds, or whatever) all operands. Without returning the ostream variable, you would have to type - cout << 3;
-
cout << 4;
-
cout << 5;
to achieve the same result.
- #include <iostream>
-
-
using namespace std;
-
-
-
class A
-
{
-
private:
-
int a;
-
int b;
-
-
public:
-
-
-
-
A( int i, int j) : a(i), b(j)
-
-
{
-
cout << "intitialize a = " << a << endl;
-
cout << "intitialize b = " << b << endl;
-
-
}
-
-
-
-
friend ostream& operator<<(ostream &os, const A &obj)
-
-
{
-
-
os << obj.a << obj.b ;
-
-
return os; // if i forget to return this referance.....
-
-
}
-
-
};
-
-
-
-
int main()
-
-
{
-
-
-
A *aobj = new A(5,10);
-
-
cout<< aobj;
-
-
cout<<endl;
-
-
-
-
return 0;
-
-
}
-
code as shown above...
i am getting :
my o/p : 004800f0
but expected is 510
why ? // i used vc6 IDE
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dan |
last post by:
Hi,
I would just like to know if the istream operator takes only one
parammeter(object) at a time (like z) ?
istream operator>>(istream& in, Shape &z)
Cause I keep getting error concerning the amount my operator has for bother
cin , cout operator<< and >>
thanks
Dan
|
by: Gianni Mariani |
last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the
code below:
friendop.cpp: In function `int main()':
friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,
std::char_traits<char> >& << Thing&' operator
friendop.cpp:27: candidates are: std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>&, const Thing&)
friendop.cpp:14: std::basic_ostream<_CharT, _Traits>&...
|
by: def5 |
last post by:
Hi,
what make this <<= operator ?
Code:
UCHAR x = 0 ;
x <<=6 ;
Thanks
|
by: pmatos |
last post by:
Hi all,
I'm overloading operator<< for a lot of classes. The question is about
style. I define in each class header the prototype of the overloading
as a friend. Now, where should I define the overloading of operator<<.
In the .cc of the respective class or in a file where I am overloading
operator<< for all classes?
Cheers,
|
by: lutorm |
last post by:
Hi everyone,
I'm trying to use istream_iterators to read a file consisting of pairs
of numbers. To do this, I wrote the following:
#include <fstream>
#include <vector>
#include <iterator>
using namespace std;
| |
by: mayFlower |
last post by:
Overloaded ostream << operator doesnt print first stream C++ .Net Framework.
For example :
ostream os;
os <<"Hello"<<"World"<<ends;
It will print something like:
0x127aa123World.
Is this something changed on .NET framework ??
Somehow .NET is considering the first stream as void pointer . Why Why
"_Myt& operator < <(const void *_Val) " is called?
|
by: peak |
last post by:
I have this code that I am using to create a heap
template <class t>
void BinaryHeap<t>::insert( const T &x)
{
int hole = ++ currentSize;
for ( ; hole > 1 && x < array;hole /= 2)
arrsy = arrsy;
array =x;
|
by: Eric Lilja |
last post by:
>From a book, I know the following is true for the comparison operators:
An overloaded operator that is a class member is only considered
when the operator is used with a *left* operand that is an object
of that class.
And is that also the reason why if you use class member functions for
operators << and >you have to write:
someclass << cout;
someclass >cin;
?
|
by: fungus |
last post by:
I have some classes which have a "writeTo()" function
but no operator<<. I want to fix it so that they
all have an operator<< (for consistency).
Can I do something like this:
template <class T>
DataDest& operator<<(DataDest& d, const T& t)
{
t.writeTo(d);
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |