473,246 Members | 1,298 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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 1391
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Neil | last post by:
Hello, Is there a standard or recommended way of handling deletion and removal of objects in STL. I have loads of list of classes by pointer. e.g. If I have a list<int*>, how should I delete...
3
by: seung | last post by:
How do I convert, for example, from vector to queue? Say, vector<int> a; queue<int> b = queue(a); This gives type error because there's no queue constructor that takes vector. Is there any...
1
by: Viorel Ghilas | last post by:
Hi all I have a list of Componet objects and put them in Containner, after container is disposed then components doesn't disposed I write some code example Component c = new Component(); ...
0
by: Andrés Giraldo | last post by:
Hi! I'm trying to insert a container object to a page, my intention is to group some controls, make it visible or invisible on depends of some actions... manually, i'll do that with a div......
3
by: Cowboy \(Gregory A. Beamer\) | last post by:
I have a user control that has a group of links. It is included on every page of a web application. Some of the pages are forms, and the requirements state "if any of the data has changed, you must...
2
by: Christoph Heindl | last post by:
Hi, I'm currently struggling with a design decision. One of my classes (named Attributable) has a protected member of type std::list<StrongReference<Attribute> > Basically the list has...
2
by: Sriram | last post by:
HI All, I have a problem in positioning a span within a div. In IE, the top and left attribute of inner object (span) is calculated from the container object, but in Firefox it is getting...
4
by: Gustaf | last post by:
I'm sure I've got this working before, but I always forget how to do it, because CSS isn't intuitive in this case. I got a simple 2 column layout: <div id="container"> <div id="left"> </div>...
5
by: bambam | last post by:
I wish to create a generic container object, devlist, such that devlist.method(arguments) runs as for each dev in devlist.pool: dev.method(arguments) and
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.