So, I am new here, but I need some help. I am looking to overload 'operator<<' so I can modify the behavior of 'std::cout' in 'main()'. This is really just bare-bones experiment that will be built on for some other things. What I have so far is this:
What this setup allows me to do is override the 'operator<<' function signature for a string constant. When I get into my implementation of this function, I ignore the string that arrives through the parameter 'char* str' and print my own string "Hello World" to 'stdio' using the function 'puts' which is also tied to stdio.
While this implementation works, I am not satisfied with it. I want to create my own ostream, say "mycout" which is tied to stdout rather than relying on 'puts'. Unfortunately this is where I get in over my head. iostreams are not an area of c++ that I have done much work in. I can't seem to find the C++ magic that will allow me to define the stream "mycout" and tie it to stdio. Can this be done? Can someone offer me some advice on this? It would be greatly appreciated.
I know that my desire to do this with iostreams rather than puts might be a bit arbitrary, but in addition to getting the job done, I am looking to get a better understanding of iostreams.
ostream already has a << operator implementation for const char*. I'd be quire surprised if that code you posted actually compiled and linked without any problem. It seems like it would conflict with the normal << operator.
Also, cout already is tied to stdio by default.
What exactly are you trying to do? If you want to define your own ostream, there are better ways.
I have actually compiled and run the code posted here. It prints out the line
Hello World
on the console rather than the line
hello
which was specified in the cout statement in the main program.
Yes ostream already has an implementation for operator<<. (I have been looking for the source code for that implementation but I have not found it.) I believe that it works because of the way iostream templates are setup. I have experimented, however, and do know that one can override operator<< signatures, by redefining the signatures in a local file.
The purpose of what I am working on is to modify the behavior of cout without modifying the code in which it is used.
I have actually compiled and run the code posted here.
After removing the undefined 'i' and giving main a return type, it compiles here as well.
I must say I'm a little surprised and intrigued.
I believe that it works because of the way iostream templates are setup.
Upon a bit further research... it seems to me that it's because the normal << overloads are actually part of basic_ostream, from which ostream must be derived.
I've been able to reproduce the behavior with this example:
The purpose of what I am working on is to modify the behavior of cout without modifying the code in which it is used.
This sounds ominous to me. If you don't want cout to act like cout, then why are you using cout? Use a different/custom ostream.
Or instead, aren't there ways to remap cout to a different ostream? I've never done it myself but I heard it can be done (don't have the foggiest idea how, though).
But anyway I'm still a little baffled as to what your original question is. You said this:
I want to create my own ostream, say "mycout" which is tied to stdout rather than relying on 'puts'.
The code you include in interesting, but it does not modify the basic behavior of cout which is what is what was required.
The requirements of this exercise are not of my making. The requirement is that different operator<< methods of ostream be overridden so that their the behavior of cout can be modified while existing code that uses cout remains untouched. Those are the rules... the idea is that this method would then be used as a way to modify other elements of code that for on reason or another cannot be modified (is cast in stone). I agree it sounds ominous and dangerous at best, but I am to simply looking into the matter as an experiment.
So... I have setup the bare-bones exercise posted here and managed to intercept cout in its attempt to print "hello" and instead print "Hello World". It works, and, as you saw from some junk that was not cleared out before I posted here, if the variable 'i' is declared and set to 5, cout will print:
Hello World5
This means that I have intercepted the attempt to print hello, but have not interfered with the rest of the stream that cout is building.
Now... while this solution works, it relies on the function puts from the cstdio header file which also writes to stdout. This may or may not be considered acceptable; but either way it does bring up an interesting point. Can I define a new ostream, call it mycout for fun, that is associated with stdout, that is a c++ stream, and doesn't depend on cstdio. Since I am required to intercept cout, so that its behavior can be modified, I cannot write to it from my version of ostream& operator<< (ostream& os, constchar* str)
since that would lead to infinite recursion.
Yes I am describing cout, but according to the environment stipulated by exercise, I cannot write to cout because I have been instructed to modify its behavior. I can do this with puts... but can I set up a new ostream, associate it with stdout and then write my "output" to this stream rather than calling puts.
It say cout, cerr, clog are instantiations aka objects of ostream class. So for your case, I believe if you want your own cout, you just got to code your class that implement all the methods in ostream class.