|
31 Jan 2003, 08:52
|
#1
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
C++: overloading parameters "<<" and ">>" for in and output
I'm creating a console class at the moment for simple in and output and I'd like to override the operators "<<" and ">>" to write and read from the console. However I have no idea how to do this (well, a very slight one...).
What I want is something simple like this:
int i = 3;
console << "blablabla" << i << "\n";
|
|
|
31 Jan 2003, 09:32
|
#2
|
Street Tramp
Join Date: Apr 2000
Location: Street Gutter
Posts: 341
|
Code:
type class-name::operator<<(arg list)
{
// Stuff
}
So for example:
Code:
console console::operator<<(char * pString)
{
strcat(this.consoletext, pString);
return this;
}
__________________
Chimney Pots.
|
|
|
31 Jan 2003, 09:36
|
#3
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Quote:
Originally posted by Raging.Retard
Code:
type class-name::operator<<(type somevar)
{
// Stuff
return somevar;
}
|
Then I'd need actually about err.... 10 or so overloaded functions.... can't I pull this off with a single function?
|
|
|
31 Jan 2003, 12:17
|
#4
|
I'm back baby!
Join Date: Aug 2001
Location: Minneapolis, MN
Posts: 22
|
Just wondering, whats wrong with iostream? You are making cout/cin from your example, so why not just use them?
__________________
-Tweakin
|
|
|
31 Jan 2003, 12:18
|
#5
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
Quote:
Originally posted by Tweakster
Just wondering, whats wrong with iostream? You are making cout/cin from your example, so why not just use them?
|
Is he not trying to create a game-console style thing? That's how I interpreted it anyway...
|
|
|
31 Jan 2003, 12:23
|
#6
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Indeed:
Code:
class CConsole
{
public:
CConsole();
~CConsole();
void open();
void close();
void consoleOut(char*, ...);
void consoleIn(char*, ...);
CConsole operator<<(char*);
CConsole operator<<(char);
CConsole operator<<(int);
CConsole operator<<(float);
private:
void writeFile();
FILE *fp;
char filename[100];
int buffercounter;
char fileBuffer[5][256];
HANDLE out;
HANDLE in;
unsigned long length;
};
I can now use this as the Iostream:
console << "Resized scene: " << width << " x " << height << "\n";
|
|
|
31 Jan 2003, 15:33
|
#7
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
This going to be a GPL'd engine Struct, or are you releasing it?
Or is it just a learning experience like?
|
|
|
31 Jan 2003, 16:49
|
#8
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
It's for my next game that I have in the back of my head: POOKIES...
It still needs a lot of work though (a few more months prolly) and I'm not sure if I will release the code. I'll see when the time is there and if it's ever going to be finished.
|
|
|
31 Jan 2003, 17:27
|
#9
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
You could subclass ostream.
|
|
|
31 Jan 2003, 18:45
|
#10
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
<< >> are bitshift operators, and any overloading of them to do anything else is dumb, confusing and non-inituative (this includes cin and cout).
True story.
|
|
|
31 Jan 2003, 19:04
|
#11
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
The English language is dumb, confusing and non-intuitive (this includes formal English and IRC chat).
True story!
|
|
|
31 Jan 2003, 19:20
|
#12
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
Quote:
Originally posted by Nodrog
<< >> are bitshift operators, and any overloading of them to do anything else is dumb, confusing and non-inituative (this includes cin and cout).
True story.
|
nah.... it's (for me) a lot easier and more intuitive to write
"OUTPUT << "blabla" << i << " x " << j;
then
OUTPUT.write("blabla %i x %i" ,i ,j);
This way I don't have to think twice how many parameters I'm giving, and of what type they are.
OK OK, there are other ways with overloading and stuff, but I find this easier. Plain and simple!
|
|
|
31 Jan 2003, 19:52
|
#13
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Quote:
Originally posted by queball
The English language is dumb, confusing and non-intuitive (this includes formal English and IRC chat).
True story!
|
qui s'inquiète?
|
|
|
31 Jan 2003, 20:13
|
#14
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
Quote:
Originally posted by Nodrog
qui s'inquiète?
|
If that's neither Italiano nor Portugesa (lo los Espanoles) no sé que significa esó.
|
|
|
31 Jan 2003, 20:17
|
#15
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Clearly, it's French for "who cares?".
|
|
|
25 Feb 2003, 20:23
|
#16
|
Guest
|
Hi, registered so I could reply to this
The best way to do this is to overload the same set of underlying base functions that the standard streams cin, cout, cerr, etc all use. That means overloading your console class from std::iostream, and then creating an overloaded std::streambuf that belongs to the console. This way you get access to all the lovely features of iostreams, including localisation support, base changing, automatic formatting of new types, etc etc etc.
I wrote an article about it a few years ago for multi-user text games where you want the character's object to be an ostream so you can do stuff like
ch <<"You have "<<ch->exp<<" experience points." << endl;
And it's not hard to extend this to include input streams too, so you can get input from the character as well.
Diz
|
|
|
25 Feb 2003, 21:50
|
#17
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Cool article.
One small point,
Code:
char_data()
: std::ostream(new netbuf)
{
rdbuf()->set_char(this);
}
Surely the value of rdbuf() needs to be cast to a netbuf *?
|
|
|
26 Feb 2003, 09:54
|
#18
|
Guest
|
Quote:
Originally posted by queball
One small point,
Code:
char_data()
: std::ostream(new netbuf)
{
rdbuf()->set_char(this);
}
Surely the value of rdbuf() needs to be cast to a netbuf *? [/b]
|
Indeed yes. Congratulations for finding the deliberate mistake.
*gets coat*
I hadn't tested it this way before, I was using a local netbuf pointer stored in char_data. I tidied it up for the article, and then introduced a bug in the process.
|
|
|
14 Mar 2003, 13:35
|
#19
|
Registered User
Join Date: Jan 2003
Location: Australia
Posts: 30
|
Quote:
Originally posted by Structural Integrity
Then I'd need actually about err.... 10 or so overloaded functions.... can't I pull this off with a single function?
|
I may be missing something - but why not use a template class then u only need do it once.
__________________
If I knew what I was doing then it wouldn't be called research.
|
|
|
|
All times are GMT +1. The time now is 04:34.
| |