Question about the operator<<

  • Thread starter Yudan Yi \(OSU\)
  • Start date
Y

Yudan Yi \(OSU\)

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
 
B

Bob Hairgrove

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.
 
R

roberts.noah

Yudan said:
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.
 
D

Dietmar Kuehl

Yudan said:
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::eek:stream::xalloc();

std::eek:stream& mode1(std::eek:stream& out) {
out.iword(key) = 1;
return out;
}
std::eek:stream& mode2(std::eek:stream& 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";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top