overloading cout

J

Joe C

I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?

Thanks,

Joe
 
C

Cy Edmunds

Joe C said:
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?

Thanks,

Joe


Well, you can't "overload cout" but there are a lot of functions or functors
which you could write. For instance:

void write_at(int x, int y, const std::string &text, std::eek:stream &os =
std::cout);

template <typename THING>
void write_at(int x, int y, const THING &thing, std::eek:stream &os =
std::cout);

class write_at
{
public:
write_at(std::eek:stream &);
template <typename THING>
void operator (int x, int y, const THING &);
private:
std::eek:stream & m_os;
};
 
H

Heinz Ozwirk

Joe C said:
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?

The C++ style to do such things would be

cout << locate(x,y) << "Some text"

You can write an IO manipulator for that, then it would work with all other
kinfs of streams, even those not based on char; or you can simply change
void locate(...) {...} to char const* locate(...){... return "";}

HTH
Heinz
 
G

Greg

Joe said:
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?

Thanks,

Joe

The easiest approach is to define an operator<< for the type of object
being output. As an example, we'll declare our own Coordinate struct
but you could just as easily use a preexisting Coordinate type:

struct Coordinate
{
Coordinate( long inX, long inY)
: x(inX), y(inY)
{
}

long y;
long x;
};

then we need only define the operator<<() for our Coordinate type:

std::eek:stream& operator<<( std::eek:stream& lhs, const Coordinate& rhs)
{
lhs << rhs.x << "," << rhs.y;
return lhs;
}

then we can output the Coordinate type as follows:

std::cout << Coordinate(x, y) << "some text";

This example takes advantage of Coordinate's constructor to build the
object in place.

Greg
 
J

Jim Langston

----- Original Message -----
From: "Greg" <[email protected]>
Newsgroups: comp.lang.c++
Sent: Monday, July 04, 2005 12:20 AM
Subject: Re: overloading cout

The easiest approach is to define an operator<< for the type of object
being output. As an example, we'll declare our own Coordinate struct
but you could just as easily use a preexisting Coordinate type:

struct Coordinate
{
Coordinate( long inX, long inY)
: x(inX), y(inY)
{
}

long y;
long x;
};

then we need only define the operator<<() for our Coordinate type:

std::eek:stream& operator<<( std::eek:stream& lhs, const Coordinate& rhs)
{
lhs << rhs.x << "," << rhs.y;
return lhs;
}

then we can output the Coordinate type as follows:

std::cout << Coordinate(x, y) << "some text";

This example takes advantage of Coordinate's constructor to build the
object in place.

Greg

Yes, but I dont' think this resolves the original problem, in that I believe
the locate(x,y) function has to be called. so shouldn't the << override
actually be:

std::eek:stream& operator<<( std::eek:stream& lhs, const Coordinate& rhs)
{
locate(x, y);
return lhs;
}

or

std::eek:stream& operator<<( std::eek:stream& lhs, const Coordinate& rhs)
{
COORD cur = { rhs.x, rhs.y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
return lhs;
}
 
M

msalters

Joe C schreef:
I'd like to have better control of text output to the console. I'm using
Windows and have a little (non-standard) function to help me out:

#include <windows.h>
void locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}

Which is easy enough to use:
locate(x,y); cout << "text here";

However...(and this is why the question is relevant in this group)...
I would prefer to use cout like:

cout(x,y) << "text here";

Can anyone instruct me on how to do this?

It wouldn't exactly be std::cout, but my::cout. That nicely solves the
name problem:

#include <iostream>
#include <iosfwd>
namespace my {
void locate(int x, int y );
std::eek:stream& cout( int x, int y ) {
locate(x,y);
return std::cout;
}
}

my::cout << "Hello, world" << std::endl;

HTH,
Michiel Salters
 
J

Joe C

msalters said:
Joe C schreef:

It wouldn't exactly be std::cout, but my::cout. That nicely solves the
name problem:

#include <iostream>
#include <iosfwd>
namespace my {
void locate(int x, int y );
std::eek:stream& cout( int x, int y ) {
locate(x,y);
return std::cout;
}
}

my::cout << "Hello, world" << std::endl;

HTH,
Michiel Salters

Thanks Michiel and the others who responded. This namespace solution is
what I am looking for, and the other solutions helped me better understand
the use of overloaded inserters and extracters.
 
M

mango_maniac

test prog follows:

#include <iostream>
#include <iosfwd>

namespace my {
void locate(int x, int y );
std::eek:stream& cout( int x = -1, int y = -1 ) {
if(~x || ~y){
locate(x,y);
}
return std::cout;
}
void cls();
void wait(){std::cin.get();}

}



using namespace my;



int main(){
cout() << "my::cout() behaves just like std::cout\n";
cout() << "with letters following the cursor";

cout(20,12) << "but you can use the function with arguments";
cout(20,13) << "to print to arbitrary coordinates";
wait();
cls();
cout(0,0) << "All";
wait();
cout(76, 0) << "you";
wait();
cout(75, 23) << "need";
wait();
cout(0,23) << "is";
wait();
cout(38, 12) << "love";


wait();
cls();

cout(25, 12) << "goodbye cruel world";


cout(0,24) << "Press Enter to continue";
std::cin.get();

int t=clock();
for(int z=0; z<100; z++){
cout(z%80, z%25) << z;
cls();
}
t=clock()-t;
cout()<< t << "secs";
cls();
int t2=clock();
for(int z=0; z<100; z++){
cout(z%80, z%25) << z;
system("cls");
}
t2 = clock() - t2;
cls();
cout()<< t << std::endl << t2 ;

wait();
return 0;

}


#include <windows.h>

void my::locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}


#include <string>
void my::cls(){
static const std::string b(2000, ' ');
my::cout(0,0) << b;
my::locate(0,0);
}
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top