473,612 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator << and >>

63 New Member
Okay, I’m just a little confused on exactly what the system is doing when I say:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. class test
  4. {
  5. private:
  6.     int i;
  7. public:
  8.     friend std::ostream& operator<< (std::ostream& o, test t);
  9.     test()
  10.     {
  11.         i = 10;
  12.     }
  13. };
  14. std::ostream& operator<< (std::ostream& o, test t)
  15. {
  16.     return o << t.i <<std::endl;
  17. }
  18.  
  19. int main()
  20. {
  21.     test testing;
  22.     std::cout << testing;
  23.     return 0;
  24. }
  25.  
I get that I’m overloading an operator, but whose operator am I overloading.

It’s not overloading anything in test, and it’s not overloading anything in any other class because it’s not member of any class. It’s just a friend of a class.

Is it overloading cout or some other buffer? If so, then what? how the heck? I dont know??????????? ?????????????
Dec 6 '06 #1
6 1796
DeMan
1,806 Top Contributor
your overloading << (or >>)
Dec 6 '06 #2
DeMan
1,806 Top Contributor
<< and >> do some interesting bitwise operations.

Try
Expand|Select|Wrap|Line Numbers
  1. int x=1<<5;
  2. printf("What happens here %d", x);
  3.  
  4. int y=x>>4;
  5. printf("or here %d",y);
  6.  
Can you see what's going on? See if you can figure it out, if you can't post back.
Dec 6 '06 #3
iLL
63 New Member
<< and >> do some interesting bitwise operations.

Try
Expand|Select|Wrap|Line Numbers
  1. int x=1<<5;
  2. printf("What happens here %d", x);
  3.  
  4. int y=x>>4;
  5. printf("or here %d",y);
  6.  
Can you see what's going on? See if you can figure it out, if you can't post back.
It moves bits to the left and right. Very cool (!), didn’t know you could do that.

your overloading << (or >>)
I know I am. But for what class? It isn’t referring to any classes.
Dec 6 '06 #4
thefarmer
55 New Member
It moves bits to the left and right. Very cool (!), didn’t know you could do that.



I know I am. But for what class? It isn’t referring to any classes.

hi there,

your overloading this part, you should not type this in this manner

by the way "return o" is this "0" or letter "o", that's also a mistake
delete << t.i <<std::endl; put that on the other statement

{
return o << t.i <<std::endl;
}

ok,

regards,
Dec 7 '06 #5
iLL
63 New Member
hi there,

your overloading this part, you should not type this in this manner

by the way "return o" is this "0" or letter "o", that's also a mistake
delete << t.i <<std::endl; put that on the other statement

{
return o << t.i <<std::endl;
}

ok,

regards,
Noooo……..

The thing is it SHOULD be written in that manner. If you took the time to compile it, it works flawlessly.

The “return o” isn’t returning the letter ‘o’ or the number zero (0); o is an object of an ostream buffer. It’s a variable.

And I’m not sure what you mean by the “other statement”



I found in one of my textbooks that I am overloading ostream. However, it doesn’t follow the C++ norm, and I’m still having a hard time wrapping my mind around it. I don’t like doing something for the simple fact of knowing that is how it is done. I like to know what is going on.
Dec 15 '06 #6
Ganon11
3,652 Recognized Expert Specialist
I believe that you are overloading the insertion operator (<<) in the ostream class (specifically, std::cout) - but only when inserting objects of class test. In other words, if you have the statement

Expand|Select|Wrap|Line Numbers
  1. std::cout << 24 << " is a cool number in my class test: " << myTestClassObject << std::endl;
std::cout will work normally as it prints the integer 24 and the string. But when it comes to myTestClassObje ct, it detects that you have overloaded the << operator, so it instead follows your directions for printing. once done, cout moves onto std::endl, which it knows how to process.
Dec 15 '06 #7

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

Similar topics

2
3208
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are writing one object in C# which will take the entire table tag contents and renders. Ie., we need to pass "<table>………… <thead>……</thead>. <tr>.<td> <td>..<tr>.<td> <td> </table>" content to
2
10554
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script type="text/javascript"> <!]> </script> <script type="text/javascript"
2
2518
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
4
2217
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
3
2034
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
0
1399
by: ma740988 | last post by:
Consider #include <iostream> #include <string> #include <map> using namespace std; struct dstream // data_stream class {
1
2037
by: ±èÀçȲ | last post by:
//this code generates the error. uint a=1,b=2; Console.WriteLine(a << b); Console.WriteLine(a >> b); What problem does "uint type" have.?
3
3366
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt, &gt ,&ltCompany&gt to display '<', '>' and '<Company>' respectively. But is there any freeware code available which could implement the above functionality without having to use &gt,&lt and such stuff???
3
1896
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the input and store in the list and finally sort the list according to the brand of cellphone. I learn C++ by myself. Therefore, i hope someone can guide me in this matter. I not asking for solution but guideline.
0
8162
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
8565
jinu1996
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...
0
7039
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...
1
6076
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4045
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...
0
4109
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1413
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.