#include <queue>
#include <string>
#include <iostream>
using namespace std;
class Foo
{
public:
string data;
};
class Holder
{
public:
void add_to_queue()
{
Foo f;
f.data = "Sup!";
holder.push(f); // Bad?
}
queue<Foo> holder;
};
int main()
{
Holder h;
h.add_to_queue();
h.add_to_queue();
return 0;
}
When I create a Foo object inside Holder::add_to_queue(), that's a
temporary variable. And then when I add it to the queue, isn't that
bad? Or does the push create a new copy of the Foo object and stick
that inside the queue?
(wondering if I should use pointers or references instead of objects)
Joe 6 1278
Joe Van Dyk wrote: #include <queue> #include <string> #include <iostream>
using namespace std;
class Foo { public: string data; };
class Holder { public: void add_to_queue() { Foo f; f.data = "Sup!"; holder.push(f); // Bad? } queue<Foo> holder; };
int main() { Holder h; h.add_to_queue(); h.add_to_queue(); return 0; }
When I create a Foo object inside Holder::add_to_queue(), that's a temporary variable. And then when I add it to the queue, isn't that bad? Or does the push create a new copy of the Foo object and stick that inside the queue?
One of the requirements of the standard containers is that elements
must be copy constructable because they do indeed make a copy. This is
the reason why you can't put a std::auto_ptr in such containers (since
std::auto_ptrs have destructive copy semantics).
(wondering if I should use pointers or references instead of objects)
You can use pointers in containers, but you'll either need to use a
smart pointer that supports copy construction (e.g.
std::tr1::shared_ptr, aka boost::shared_ptr) or delete the items
yourself -- the container will deallocate the space used for a raw
pointer that it holds but not the pointee of that pointer.
Cheers! --M
Joe Van Dyk <jo********@boeing.com> wrote: #include <queue> #include <string> #include <iostream>
using namespace std;
class Foo { public: string data; };
class Holder { public: void add_to_queue() { Foo f; f.data = "Sup!"; holder.push(f); // Bad? } queue<Foo> holder; };
int main() { Holder h; h.add_to_queue(); h.add_to_queue(); return 0; }
When I create a Foo object inside Holder::add_to_queue(), that's a temporary variable. And then when I add it to the queue, isn't that bad? Or does the push create a new copy of the Foo object and stick that inside the queue?
The latter. Your code is fine the way it is.
(wondering if I should use pointers or references instead of objects)
If your actual code is very similar to the example code above, I see
no reason for using pointers or references. When passing a Holder or a
Foo object around, you might want to do that by const-reference (this is
how you pass the temporary Foo to queue<Foo>::push, for example).
hth
--
jb
(reply address in rot13, unscramble first)
Jakob Bieling wrote: Joe Van Dyk <jo********@boeing.com> wrote:
#include <queue> #include <string> #include <iostream>
using namespace std;
class Foo { public: string data; };
class Holder { public: void add_to_queue() { Foo f; f.data = "Sup!"; holder.push(f); // Bad? } queue<Foo> holder; };
int main() { Holder h; h.add_to_queue(); h.add_to_queue(); return 0; }
When I create a Foo object inside Holder::add_to_queue(), that's a temporary variable. And then when I add it to the queue, isn't that bad? Or does the push create a new copy of the Foo object and stick that inside the queue?
The latter. Your code is fine the way it is.
(wondering if I should use pointers or references instead of objects)
If your actual code is very similar to the example code above, I see no reason for using pointers or references. When passing a Holder or a Foo object around, you might want to do that by const-reference (this is how you pass the temporary Foo to queue<Foo>::push, for example).
hth
Thanks.
In actuality, Holder::add_to_queue takes a string and creates a Foo
object based on that string. Then that Foo object gets pushed into the
queue.
Let's rename Foo to BaseMessage.
So, I have several classes that inherit from BaseMessage. So,
JoesMessage, JimsMessage, etc. The contents of the String that gets
passed to Holder::add_to_queue needs to be examined somehow and the
appropriate object needs to be constructed and inserted into the Queue
(ideas welcome on an elegant way to do that).
In that case, the queue would need to contain pointers to BaseMessage
objects, right? As there would be JoesMessages, JimMessages, etc all in
the same queue.
Joe Van Dyk <jo********@boeing.com> wrote: Jakob Bieling wrote:
Joe Van Dyk <jo********@boeing.com> wrote:
#include <queue> #include <string> #include <iostream>
using namespace std;
class Foo { public: string data; };
class Holder { public: void add_to_queue() { Foo f; f.data = "Sup!"; holder.push(f); // Bad? } queue<Foo> holder; };
int main() { Holder h; h.add_to_queue(); h.add_to_queue(); return 0; }
If your actual code is very similar to the example code above, I see no reason for using pointers or references. When passing a Holder or a Foo object around, you might want to do that by const-reference (this is how you pass the temporary Foo to queue<Foo>::push, for example).
In actuality, Holder::add_to_queue takes a string and creates a Foo object based on that string. Then that Foo object gets pushed into the queue.
Let's rename Foo to BaseMessage.
So, I have several classes that inherit from BaseMessage. So, JoesMessage, JimsMessage, etc. The contents of the String that gets passed to Holder::add_to_queue needs to be examined somehow and the appropriate object needs to be constructed and inserted into the Queue (ideas welcome on an elegant way to do that).
If you must use strings to determine which object to construct, I
guess the method you outlined (parse string and construct based on
parse-result) is ok.
Reminds me of a script parser, where you have a script-type-name as
a string and need to construct a different object (representing the
type) depending on the string. In that case, I would do as you said.
In that case, the queue would need to contain pointers to BaseMessage objects, right? As there would be JoesMessages, JimMessages, etc all in the same queue.
Right, you can only work with pointers here. Note that the queue
will not delete the memory the pointers point to. It will only manage
the memory needed to store the pointers themselves.
hth
--
jb
(reply address in rot13, unscramble first)
Jakob Bieling wrote: Joe Van Dyk <jo********@boeing.com> wrote:
Jakob Bieling wrote:
Joe Van Dyk <jo********@boeing.com> wrote: #include <queue> #include <string> #include <iostream>
using namespace std;
class Foo { public: string data; };
class Holder { public: void add_to_queue() { Foo f; f.data = "Sup!"; holder.push(f); // Bad? } queue<Foo> holder; };
int main() { Holder h; h.add_to_queue(); h.add_to_queue(); return 0; }
If your actual code is very similar to the example code above, I see no reason for using pointers or references. When passing a Holder or a Foo object around, you might want to do that by const-reference (this is how you pass the temporary Foo to queue<Foo>::push, for example).
In actuality, Holder::add_to_queue takes a string and creates a Foo object based on that string. Then that Foo object gets pushed into the queue.
Let's rename Foo to BaseMessage.
So, I have several classes that inherit from BaseMessage. So, JoesMessage, JimsMessage, etc. The contents of the String that gets passed to Holder::add_to_queue needs to be examined somehow and the appropriate object needs to be constructed and inserted into the Queue (ideas welcome on an elegant way to do that). If you must use strings to determine which object to construct, I guess the method you outlined (parse string and construct based on parse-result) is ok.
It's an XML string, joy! Reminds me of a script parser, where you have a script-type-name as a string and need to construct a different object (representing the type) depending on the string. In that case, I would do as you said.
In that case, the queue would need to contain pointers to BaseMessage objects, right? As there would be JoesMessages, JimMessages, etc all in the same queue.
Right, you can only work with pointers here. Note that the queue will not delete the memory the pointers point to. It will only manage the memory needed to store the pointers themselves.
Is there a good idiom for popping a pointer to an element from the
queue, looking at the element, and then deleting the element?
Joe
Joe Van Dyk <jo********@boeing.com> wrote: Jakob Bieling wrote:
Joe Van Dyk <jo********@boeing.com> wrote:
[..] the queue would need to contain pointers to BaseMessage objects, right? As there would be JoesMessages, JimMessages, etc all in the same queue.
Right, you can only work with pointers here. Note that the queue will not delete the memory the pointers point to. It will only manage the memory needed to store the pointers themselves.
Is there a good idiom for popping a pointer to an element from the queue, looking at the element, and then deleting the element?
I do not understand what you mean. Why not use 'queue<T>::front' to
get the top element, 'queue<T>::pop' to remove it from the queue and
then delete it (through the pointer you just got from 'front'):
regards
--
jb
(reply address in rot13, unscramble first) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pjotr Wedersteers |
last post by:
This may be a silly question, but I was wondering.
If 10 clients fill out a form on my page simultaneaously and hit submit, how
does my (Apache2.0.50/PHP4.3.8) server exactly ensure each gets...
|
by: Jenny |
last post by:
Hi you al
I have two very silly question
The first one is
In vb 6.0 you can add a procedure, sub or function, by clicking add procedure from the tools item on the menuba
I cannot seem to find...
|
by: musosdev |
last post by:
I think I'm having a dim day and should just go back to bed, but alas, I cant
do that....
I'm writing a peice of code to create XHTML compliant documents using
System.IO, there's probably an...
|
by: Adam Honek |
last post by:
Hi,
I've looked over and over and in MSDN 2005 and just can't find what
is a really simple answer.
How do I select the first value in a combo box to be shown?
I tried the .selectedindex -1...
|
by: Matt |
last post by:
I've asked this question to some developers that are much more experienced
than I, and gotten different answers --- and I can't find anything about it
in the documentation.
Dim vs. Private as a...
|
by: Willem Voncken |
last post by:
Hi guys,
might be a silly question, but i'm wondering whether it is even possible to
generate access violations when using C#?
I'm new at c# programming, but i have some experience with c++....
|
by: Pontifex |
last post by:
Hi all,
Has anyone devised a simple method for passing
a string to an external script?
For instance, suppose I want to produce a title bar
that is very fancy and I don't want my main HTML...
|
by: toton |
last post by:
Hi,
This is a silly question related to syntax only.
I have a template class with template member function, How to write it
separately in a file (not translation unit, just want to separate...
|
by: Marcin Kasprzak |
last post by:
Hello Guys,
Silly question - what is the most elegant way of compiling a code similar
to this one?
<code>
typedef struct a {
b_t *b;
} a_t;
typedef struct b {
|
by: Ben Bacarisse |
last post by:
Newbie <newbie@gmail.comwrites:
<snip>
The version you posted in comp.programming has the correct loop. Why
change it?
--
Ben.
|
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: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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: 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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |