Container << Object

G

gipsy boy

I made a Stack structure, as an exercise on templates.
Stack ll;

Is this used irl? : "ll << o" to push an object pointer to the end of
the list and "ll >> &o" to push it off and save the address in &o or
s'thing similar. I mean the usage of the <</>> operators..
We need to learn good style, I was wodnering if this is..good style..or
just a newbie's obsession with operator overloading.
 
K

Karl Heinz Buchegger

gipsy said:
I made a Stack structure, as an exercise on templates.
Stack ll;

Is this used irl? : "ll << o" to push an object pointer to the end of
the list and "ll >> &o" to push it off and save the address in &o or
s'thing similar. I mean the usage of the <</>> operators..
We need to learn good style, I was wodnering if this is..good style..or
just a newbie's obsession with operator overloading.

Well. You could define this operators. Nobody is hindering you.

On the otherhand you can define a class MyInteger, where operator+
is defined to mulitply two MyIntegers. Nobody is hindering you.

So for this reason, there is a rule of thumb: When overloading operators,
do as int do.

For int op<< and op>> are defined to output and input from streams. C++
programmers are used to this semantic, so I wouldn't redefine the meaning
of op<< and op>> for some other classes.
 
V

Vyacheslav Kononenko

Karl said:
Well. You could define this operators. Nobody is hindering you.

On the otherhand you can define a class MyInteger, where operator+
is defined to mulitply two MyIntegers. Nobody is hindering you.

So for this reason, there is a rule of thumb: When overloading operators,
do as int do.

For int op<< and op>> are defined to output and input from streams. C++
programmers are used to this semantic, so I wouldn't redefine the meaning
of op<< and op>> for some other classes.
Hmm I always thaught that << for int means bitwise shift... :)
 
R

Richard Herring

gipsy boy said:
I made a Stack structure, as an exercise on templates.
Stack ll;

Is this used irl? : "ll << o" to push

Not commonly, but there's no apparently overwhelming reason not to. It
has the advantage that (as with the streaming operators) << associates
the right way so that you can make ll << a << b do what you expect.

Questions to ask yourself is: is this usage of << consistent with a
user's "intuitive" understanding of how it's used elsewhere? will the
paired operator >> also be consistent? is there an alternative operator
which would do as well or better? will the rules of operator priority
and association work as the user expects?
an object pointer to the end of the list

But are you sure you mean "pointer" there? Is 'o' a pointer or an
object? Pushing (a copy of) the thing itself is OK; pushing a pointer to
it is asking for trouble.
and "ll >> &o" to push
"pop"

it off and

Here's the problem: that innocent word "and". It turns out that there
are problems with exception safety if you try to write a function which
copies the top element of the stack _and_ removes it in the same
operation.

http://www.awprofessional.com/content/downloads/meyerscddemo/DEMO/MAGAZIN
E/SU_FRAME.HTM
http://www.awprofessional.com/content/downloads/meyerscddemo/DEMO/MAGAZIN
E/CA_FRAME.HTM
http://www.gotw.ca/gotw/008.htm

Bottom line: you can't simultaneously have a robust container _and_ give
save the address in &o or s'thing similar.

I don't like the look of that & or the word "address". What actually
happens to 'o' here?
I mean the usage of the <</>> operators..
We need to learn good style, I was wodnering if this is..good style..or
just a newbie's obsession with operator overloading.

Look how std::stack does it. They use push(o) to push an item, top() to
get a reference to the top item, pop() to lose the top item.
 
J

Jonathan Mcdougall

I made a Stack structure, as an exercise on templates.
Stack ll;

Is this used irl? : "ll << o" to push an object pointer to the end of
the list and "ll >> &o" to push it off and save the address in &o or
s'thing similar. I mean the usage of the <</>> operators..
We need to learn good style, I was wodnering if this is..good style..or
just a newbie's obsession with operator overloading.

Be very careful when overloading operators. They are a powerful feature
and are easily over/misused. In particular, the use of operators << andpushing values in chain on a stack is quite rare, I would believe this
is not a good idea.

When overloading operators, think about what a programmer expects when
using an operator. The operator + represents adding two things together
and operator () represents a function call. Operator <<, in the context
of a stream, means "put into". Your version would slightly change that
meaning and I don't think the synctactic change makes enough difference
to justify that change.

Jonathan
 
H

Howard

Is this used irl? :

You seem to have gotten good answers, but I'm confused by the question!
What does that question "Is this used irl?" mean? (I guess "irl" is an
acronym of some sort? BTW, imnsho, fwiw, I hate acronyms! :))

-Howard
 
R

Rob Williscroft

Howard wrote in news.ops.worldnet.att.net in comp.lang.c++:
You seem to have gotten good answers, but I'm confused by the question!
What does that question "Is this used irl?" mean? (I guess "irl" is an
acronym of some sort? BTW, imnsho, fwiw, I hate acronyms! :))

In Real Life.

Rob.
 
G

gipsy boy

Richard said:
But are you sure you mean "pointer" there? Is 'o' a pointer or an
object? Pushing (a copy of) the thing itself is OK; pushing a pointer to
it is asking for trouble.


I don't like the look of that & or the word "address". What actually
happens to 'o' here?

Yeah, I meant objects, for both sides, just got a little ahead of myself
there. I'm still mixing up C and C++ a lot too, I keep wanting to work
with pointers to keep my objects small, which is kind of silly I know.

The chaining was the only reason why I'd do it with << operators, but
apparently it's not a good idea..I'll just act normal.
I will however make an ArrayList class too for which the [] operators
get the elementAt(i), but that's something else of course.
Thx for the guidance everyone..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,976
Members
47,536
Latest member
MistyLough

Latest Threads

Top