By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,882 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,882 IT Pros & Developers. It's quick & easy.

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

P: 9
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
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
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

P: 9
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

P: 9
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

Post your reply

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