469,890 Members | 1,967 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Container << Object

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.
--
- gipsy boy
Jul 22 '05 #1
7 1220
gipsy boy wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Karl Heinz Buchegger wrote:
gipsy boy wrote:
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.

Hmm I always thaught that << for int means bitwise shift... :)

--
Regards,
Slava

Jul 22 '05 #3
In message <AP*********************@phobos.telenet-ops.be>, gipsy boy
<x@x.pi> writes
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/conten...o/DEMO/MAGAZIN
E/SU_FRAME.HTM
http://www.awprofessional.com/conten...o/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
<< and >> the obvious semantics.
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.

--
Richard Herring
Jul 22 '05 #4
> 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 << and
with streams allows a simple way to chain operations. Since

pushing 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
Jul 22 '05 #5

"gipsy boy" <x@x.pi> wrote in message
news:AP*********************@phobos.telenet-ops.be...
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
Jul 22 '05 #6
Howard wrote in news:hrMod.961614$Gx4.643263@bgtnsc04-
news.ops.worldnet.att.net in comp.lang.c++:

"gipsy boy" <x@x.pi> wrote in message
news:AP*********************@phobos.telenet-ops.be...
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! :-))


In Real Life.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7
Richard Herring wrote:
In message <AP*********************@phobos.telenet-ops.be>, gipsy boy
<x@x.pi> writes
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

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.
...
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?


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..

--
- gipsy boy
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Viorel Ghilas | last post: by
reply views Thread by Andrés Giraldo | last post: by
3 posts views Thread by Cowboy \(Gregory A. Beamer\) | last post: by
2 posts views Thread by Christoph Heindl | last post: by
4 posts views Thread by Gustaf | last post: by
5 posts views Thread by bambam | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.