473,762 Members | 5,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about the operator<<

I have a question to define a friend operator<< for a class.
for example, I can define
friend ostream& operator<<(ostr eam& 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<<(ostr eam& 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 1596
On Wed, 7 Dec 2005 16:43:42 -0500, "Yudan Yi \(OSU\)" <yi***@osu.ed u>
wrote:
I have a question to define a friend operator<< for a class.
for example, I can define
friend ostream& operator<<(ostr eam& 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<<(ostr eam& 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**********@Ho me.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<<(ostr eam& 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<<(ostr eam& 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::x alloc();

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

friend ostream& operator<<(ostr eam& 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.co m> <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
1650
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
2049
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;
3
2954
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 FUNCTION!!
14
2339
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;
4
2060
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 following will be printed A 3 2
3
1592
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; // here x contains "004400C8pluto"
4
2504
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
1885
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
8
1705
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 <libsomelib.h>
0
9378
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
10137
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...
0
9989
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...
1
9927
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
9812
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...
1
7360
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
6640
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();...
0
5268
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...
3
2788
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.