iLL 63
New Member
Okay, I’m just a little confused on exactly what the system is doing when I say: -
#include <iostream>
-
-
class test
-
{
-
private:
-
int i;
-
public:
-
friend std::ostream& operator<< (std::ostream& o, test t);
-
test()
-
{
-
i = 10;
-
}
-
};
-
std::ostream& operator<< (std::ostream& o, test t)
-
{
-
return o << t.i <<std::endl;
-
}
-
-
int main()
-
{
-
test testing;
-
std::cout << testing;
-
return 0;
-
}
-
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??????????? ?????????????
6 1796 DeMan 1,806
Top Contributor
your overloading << (or >>)
DeMan 1,806
Top Contributor
<< and >> do some interesting bitwise operations.
Try -
int x=1<<5;
-
printf("What happens here %d", x);
-
-
int y=x>>4;
-
printf("or here %d",y);
-
Can you see what's going on? See if you can figure it out, if you can't post back.
iLL 63
New Member
<< and >> do some interesting bitwise operations.
Try -
int x=1<<5;
-
printf("What happens here %d", x);
-
-
int y=x>>4;
-
printf("or here %d",y);
-
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.
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,
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.
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 - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
by: Donald Firesmith |
last post by:
I am having trouble having Google Adsense code stored in XSL converted
properly into HTML. The <> unfortunately become < and > and then
no longer work.
XSL code is:
<script type="text/javascript">
<!]>
</script>
<script type="text/javascript"
|
by: gbgbgbgb |
last post by:
Hi,
I have a definition
bool operator<(string s_s, string s_t)
{
....
}
and a variable
list<string> concomp;
|
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: 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;
| |
by: ma740988 |
last post by:
Consider
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct dstream // data_stream class
{
|
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.?
|
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 <, > ,<Company> to display '<', '>' and '<Company>' respectively.
But is there any freeware code available which could implement the above functionality without having to use >,< and such stuff???
|
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.
|
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: 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: 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...
|
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: 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
|
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...
| |