473,714 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what will happen, if we forget to return ostream referance in operator<<() function ?

9 New Member
what will happen, if we forget to return ostream referance ?

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.  
  4.    Private:
  5.           int _a;
  6.           int  _b;
  7.  
  8.   public:
  9.  
  10.     A( int i, int j) : a(i), b(j)
  11.      {}
  12.  
  13.    friend ostream& operator<<(ostream &os, const A &obj)
  14.    {
  15.         os << obj._a << obj._b << endl;
  16.         return os;        //  if i forget to return this referance.....
  17.    }
  18. };
  19.  
  20. int main()
  21. {
  22.  
  23. A aobj = new A(5,10);
  24.  
  25. cout<< aobj;
  26.  
  27. return 0;
  28. }
  29.  
O/P = 5 10 //Answer

OR

can we use like this
Expand|Select|Wrap|Line Numbers
  1. friend void operator<<(ostream &os, const A &obj)
  2.    {
  3.         os << obj._a << obj._b << endl;
  4.  
  5.    }
  6.  
will the ouput will be the same.....
Jun 14 '07 #1
3 1717
weaknessforcats
9,208 Recognized Expert Moderator Expert
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;
Jun 14 '07 #2
ProgrammerMatt
9 New Member
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
Expand|Select|Wrap|Line Numbers
  1. cout << 3;
  2. cout << 4;
  3. cout << 5;
to achieve the same result.
Jun 14 '07 #3
vimalankvk80
9 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.       class A
  7.       {
  8.          private:   
  9.                 int a;   
  10.                 int b;    
  11.  
  12.         public:
  13.  
  14.  
  15.  
  16.           A( int i, int j) : a(i), b(j)
  17.  
  18.            {
  19.               cout << "intitialize a = " << a << endl;
  20.               cout << "intitialize b = " << b << endl;
  21.  
  22.           }
  23.  
  24.  
  25.  
  26.          friend ostream& operator<<(ostream &os, const A &obj)
  27.  
  28.          {
  29.  
  30.               os << obj.a  << obj.b ;
  31.  
  32.               return os;        //  if i forget to return this referance.....
  33.  
  34.          }
  35.  
  36.       };
  37.  
  38.  
  39.  
  40.       int main()
  41.  
  42.       {
  43.  
  44.  
  45.       A *aobj = new A(5,10);       
  46.  
  47.       cout<< aobj;
  48.  
  49.       cout<<endl;
  50.  
  51.  
  52.  
  53.       return 0;
  54.  
  55.       }
  56.  
code as shown above...

i am getting :

my o/p : 004800f0

but expected is 510

why ? // i used vc6 IDE
Jun 15 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2224
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
5
5010
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>&...
5
1194
by: def5 | last post by:
Hi, what make this <<= operator ? Code: UCHAR x = 0 ; x <<=6 ; Thanks
2
2065
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,
14
2333
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;
1
1849
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?
3
1287
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;
7
2795
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; ?
11
3554
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);
0
8801
marktang
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...
0
8707
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,...
0
9314
Oralloy
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...
1
9074
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,...
0
9015
tracyyun
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...
0
7953
agi2029
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...
0
5947
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();...
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2110
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.