J
Joe Van Dyk
#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
#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