Connecting Tech Pros Worldwide Help | Site Map

overloading cout

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 05:49 AM
Joe C
Guest
 
Posts: n/a
Default overloading cout

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



  #2  
Old July 23rd, 2005, 05:49 AM
Cy Edmunds
Guest
 
Posts: n/a
Default Re: overloading cout

"Joe C" <jkc8289@bellsouth.net> wrote in message
news:ub2ye.23839$Tt.16731@bignews3.bellsouth.net.. .[color=blue]
> 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[/color]


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::ostream &os =
std::cout);

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

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

--
Cy
http://home.rochester.rr.com/cyhome/


  #3  
Old July 23rd, 2005, 05:49 AM
Heinz Ozwirk
Guest
 
Posts: n/a
Default Re: overloading cout

"Joe C" <jkc8289@bellsouth.net> schrieb im Newsbeitrag
news:ub2ye.23839$Tt.16731@bignews3.bellsouth.net.. .[color=blue]
> 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?[/color]

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


  #4  
Old July 23rd, 2005, 05:49 AM
Greg
Guest
 
Posts: n/a
Default Re: overloading cout



Joe C wrote:[color=blue]
> 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[/color]

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::ostream& operator<<( std::ostream& 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

  #5  
Old July 23rd, 2005, 05:49 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: overloading cout

----- Original Message -----
From: "Greg" <greghe@pacbell.net>
Newsgroups: comp.lang.c++
Sent: Monday, July 04, 2005 12:20 AM
Subject: Re: overloading cout

[color=blue]
>
>
> Joe C wrote:[color=green]
>> 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[/color]
>
> 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::ostream& operator<<( std::ostream& 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
>[/color]

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::ostream& operator<<( std::ostream& lhs, const Coordinate& rhs)
{
locate(x, y);
return lhs;
}

or

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


  #6  
Old July 23rd, 2005, 05:50 AM
msalters
Guest
 
Posts: n/a
Default Re: overloading cout



Joe C schreef:[color=blue]
> 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?[/color]

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::ostream& cout( int x, int y ) {
locate(x,y);
return std::cout;
}
}

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

HTH,
Michiel Salters

  #7  
Old July 23rd, 2005, 05:50 AM
Joe C
Guest
 
Posts: n/a
Default Re: overloading cout


"msalters" <Michiel.Salters@logicacmg.com> wrote in message
news:1120479700.610293.96760@g49g2000cwa.googlegro ups.com...[color=blue]
>
>
> Joe C schreef:[color=green]
>> 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?[/color]
>
> 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::ostream& cout( int x, int y ) {
> locate(x,y);
> return std::cout;
> }
> }
>
> my::cout << "Hello, world" << std::endl;
>
> HTH,
> Michiel Salters
>[/color]

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.


  #8  
Old July 23rd, 2005, 05:50 AM
mango_maniac@yahoo.com
Guest
 
Posts: n/a
Default Re: overloading cout

test prog follows:

#include <iostream>
#include <iosfwd>

namespace my {
void locate(int x, int y );
std::ostream& 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);
}

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.