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 7 1396
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
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
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
> 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
"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
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/
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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();
...
|
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......
|
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...
|
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...
|
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...
|
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>...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |