Archived

This topic is now archived and is closed to further replies.

farmersckn

How Cool Is This!?

Recommended Posts

run this program:
#include "Iostream.h"

class _print
{

public:

	_print& operator , (const char* s) { cout << s; return *this; }

} Print;

int main()
{
	Print, "Hello", "World";

	return 0;
};

 
Isn''t that a cool little tidbit? Can''t think of a real use for it, though... farmersckn

Share this post


Link to post
Share on other sites
Syntactical ease is one use I can think of. The reason why no one uses it is that operators have a tendency to hide code, and this probably scares quite a few organized coders.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why not just this instead:?

#include


class _Print
{
public:
void operator() ( const char* string )
{ std::cout << string; }
} Print;

void main()
{
Print("Hello world");
return 0;
}


Hmm...thats whats called a function object, isn''t it?

Share this post


Link to post
Share on other sites
Er...

Why not just use

#include "iostream.h"

int main(void) {
cout << "Hello World";
}

Is there really a need for all that complexity to print a string?

E


Edited by - Eight on March 21, 2001 7:57:53 AM

Share this post


Link to post
Share on other sites
I''d say there''s no _painless_ use for it. But imagine this:

You need to define a set of objects, and each object has to update its data to reflect its neighbor. For example, you could use this to define a linked list with the sequencing operator...much clearer than when you use function calls. But as any overloaded operator, it''s a syntactical nightmare.

Share this post


Link to post
Share on other sites