User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 31 Jan 2003, 08:52   #1
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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";
Structural Integrity is offline   Reply With Quote
Unread 31 Jan 2003, 09:32   #2
Raging.Retard
Street Tramp
 
Raging.Retard's Avatar
 
Join Date: Apr 2000
Location: Street Gutter
Posts: 341
Raging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant future
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.
Raging.Retard is offline   Reply With Quote
Unread 31 Jan 2003, 09:36   #3
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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?
Structural Integrity is offline   Reply With Quote
Unread 31 Jan 2003, 12:17   #4
Tweakster
I'm back baby!
 
Join Date: Aug 2001
Location: Minneapolis, MN
Posts: 22
Tweakster is an unknown quantity at this point
Just wondering, whats wrong with iostream? You are making cout/cin from your example, so why not just use them?
__________________
-Tweakin
Tweakster is offline   Reply With Quote
Unread 31 Jan 2003, 12:18   #5
pablissimo
Henry Kelly
 
pablissimo's Avatar
 
Join Date: Apr 2000
Posts: 7,374
pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
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...
pablissimo is offline   Reply With Quote
Unread 31 Jan 2003, 12:23   #6
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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";
Structural Integrity is offline   Reply With Quote
Unread 31 Jan 2003, 15:33   #7
pablissimo
Henry Kelly
 
pablissimo's Avatar
 
Join Date: Apr 2000
Posts: 7,374
pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
This going to be a GPL'd engine Struct, or are you releasing it?

Or is it just a learning experience like?
pablissimo is offline   Reply With Quote
Unread 31 Jan 2003, 16:49   #8
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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.
Structural Integrity is offline   Reply With Quote
Unread 31 Jan 2003, 17:27   #9
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
You could subclass ostream.
queball is offline   Reply With Quote
Unread 31 Jan 2003, 18:45   #10
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
<< >> 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.
Nodrog is offline   Reply With Quote
Unread 31 Jan 2003, 19:04   #11
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
The English language is dumb, confusing and non-intuitive (this includes formal English and IRC chat).

True story!
queball is offline   Reply With Quote
Unread 31 Jan 2003, 19:20   #12
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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!
Structural Integrity is offline   Reply With Quote
Unread 31 Jan 2003, 19:52   #13
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
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?
Nodrog is offline   Reply With Quote
Unread 31 Jan 2003, 20:13   #14
pablissimo
Henry Kelly
 
pablissimo's Avatar
 
Join Date: Apr 2000
Posts: 7,374
pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Quote:
Originally posted by Nodrog
qui s'inquiète?

If that's neither Italiano nor Portugesa (lo los Espanoles) no sé que significa esó.
pablissimo is offline   Reply With Quote
Unread 31 Jan 2003, 20:17   #15
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Clearly, it's French for "who cares?".
queball is offline   Reply With Quote
Unread 25 Feb 2003, 20:23   #16
DizietSma
Guest
 
Posts: n/a
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
  Reply With Quote
Unread 25 Feb 2003, 21:50   #17
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by DizietSma
an article
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 *?
queball is offline   Reply With Quote
Unread 26 Feb 2003, 09:54   #18
DizietSma
Guest
 
Posts: n/a
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.
  Reply With Quote
Unread 14 Mar 2003, 13:35   #19
MOS
Registered User
 
Join Date: Jan 2003
Location: Australia
Posts: 30
MOS is an unknown quantity at this point
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.
MOS is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 04:34.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018