473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Question about the operator<<

I have a question to define a friend operator<< for a class.
for example, I can define
friend ostream& operator<<(ostream& os, const TTest& x) { ...; return
(os); };

While I want to add more control to the output by an additional parameter,
can I do in the following way?
friend ostream& operator<<(ostream& os, const TTest& x, unsigned parameter)
{
switch (parameter)
{
case: ...
}
return (os);
};

If I can define in this way, then how can I use it? if not, how can I do it?

Thanks


Dec 7 '05 #1
3 1569
On Wed, 7 Dec 2005 16:43:42 -0500, "Yudan Yi \(OSU\)" <yi***@osu.edu>
wrote:
I have a question to define a friend operator<< for a class.
for example, I can define
friend ostream& operator<<(ostream& os, const TTest& x) { ...; return
(os); };

While I want to add more control to the output by an additional parameter,
can I do in the following way?
friend ostream& operator<<(ostream& os, const TTest& x, unsigned parameter)
{
switch (parameter)
{
case: ...
}
return (os);
};

If I can define in this way, then how can I use it? if not, how can I do it?


You can't change the signature of the function because it won't work
otherwise. How would you call it?

Maybe you can make the parameter part of TTest's class, in which case
you can test it inside the body of the operator. Since it is a friend
of TTest, it has access to all of its private and protected members.

--
Bob Hairgrove
No**********@Home.com
Dec 7 '05 #2

Yudan Yi (OSU) wrote:
I have a question to define a friend operator<< for a class.
for example, I can define
friend ostream& operator<<(ostream& os, const TTest& x) { ...; return
(os); };

While I want to add more control to the output by an additional parameter,
can I do in the following way?
No. << is a binary operator.
friend ostream& operator<<(ostream& os, const TTest& x, unsigned parameter)
{
switch (parameter)
{
case: ...
}
return (os);
};

If I can define in this way, then how can I use it? if not, how can I do it?


Define a function that takes three parameters and call it.

Dec 7 '05 #3
Yudan Yi (OSU) wrote:
While I want to add more control to the output by an additional parameter,
can I do in the following way?


No: the 'operator<<()' is a binary function. Assuming you want to
add some form of format control for 'TTest' to your stream, you
can store the format using the 'iword()' or 'pword()' members of
the stream. This could look e.g. like this:

int key = std::ostream::xalloc();

std::ostream& mode1(std::ostream& out) {
out.iword(key) = 1;
return out;
}
std::ostream& mode2(std::ostream& out) {
out.iword(key) = 2;
return out;
}
// ...

friend ostream& operator<<(ostream& os, TTest const& x)
{
switch (os.iword(key))
{
case 1: // ...
case 2: // ...
case 0: // this is the initial value of 'os.iword(key)'
default: // ...
}
return out;
}

You would use it like this:

void print(TTest const& x) {
std::cout << "default format: " << x << "\n";
std::cout << "format 1: " << mode1 << x << "\n";
std::cout << "format 2: " << mode2 << x << "\n";
}
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Dec 7 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: JustSomeGuy | last post by:
I have a double that I want to output as: (depending on its value) 1000 Bytes 1.6 Kilobyte 2.5 Megabytes ..5 Terabytes can I do this with... ostream & operator<<(ostream & o, double n)
3
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...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
14
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> ...
4
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the...
3
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends;...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
3
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
8
by: Goran | last post by:
Hi all, I have a question regarding operator <<. A lib of mine contains a class with an overloaded operator << as NON- class member. This would look like: #include <iostream> #include...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.