I found this as a solution when I was trying to find an easy way to print out a description of my objects in main() by just writing cout << object << endl; however I wasn't able to find an explanation that helped me understand what was actually happening with the streams and how everything was being processed etc.

In regards to the stack, heap and buffers, what is happening when I call my << operator in the following way:

Code:

cout << PositionObject << endl;

Is a buffer holding the "(" << p.x << ", " << p.y << ")" string I put together created in the stack when a Position object is created? or what is the sequence of events to retrieving it?

Also, if a buffer is not created, can someone give me an example of when one is created other than an array?

You've overloaded the << operator, so a specific function has been defined holding your code for the Position class. Now the compiler is able to select the appropriate function depending on the operand type. There's only one syntax (<<) but many functions.

In regards to the stack, heap and buffers, what is happening when I call my << operator in the following way:

Code:

cout << PositionObject << endl;

Is a buffer holding the "(" << p.x << ", " << p.y << ")" string I put together created in the stack when a Position object is created? or what is the sequence of events to retrieving it?

There is no buffer - as such - for the above created at instantiation of a Position object. When you call the function, cout is passed by reference as os (os in effect becomes an alias for cout in this particular case). Then, the particular sequence of stream insertion statements you've indicated is run on the os/cout stream. The stream itself has a buffer it manages but I don't think this is what you are referring to. The code for the overloaded stream insertion function is loaded into memory at time of execution of the program (I don't know if you would call this a buffer, I don't). When the function is called, several things take place, among them objects and variable used by the function are pushed onto the stack and the code pointer jumps to the start of the code for the function and execution of code takes place from that point on. When the function ends, any objects on the stack used by the function are popped off and the code pointer returns to the point where the function was called from and execution of the program can continue from where things left off last.

Don't know if that explains things for you.

Originally Posted by stariz

Also, if a buffer is not created, can someone give me an example of when one is created other than an array?

Seems to me you are unclear of the meaning of the word "buffer", at least in the context of programming. In some regards any area of memory can be considered a buffer but in programming it typically refers to a scratch/temporary holding area where data is stored until further processing can be done on said stored data. Whether an array constitutes a "buffer" or not depends on the purpose of the array.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

Depending on what type your ostream object is, it *probably* has an internal buffer, which is *probably* allocated on the heap. In the case of cout, yes there is a buffer somewhere, and each call to operator<< converts its argument to a string and adds it to the end of the buffer.

A buffer is not always required however. For example, you might write an ostream class which writes data directly to some hardware.

I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo