STL Containers store a copy?
-
Hello! My question is, do STL containers store a copy or they keep the object itself? Hence, can I insert a local variable into a container?
typedef struct{
int number;
string name; } myData;
class myClass{
void InsertData(myData &a);
void oneFunction ();
void otherFunction ();
collection m_memberCollection;
};
void oneFunction() {
myData localData;
localData.name = "Paul";
InsertData(localData);
}
void myClass::InsertData (myData &a){
a.number = 5;
m_memberCollection.push_back(a);
}
void otherFunction (){
printf ("%s %d", m_memberCollection[0].name.c_str(), m_memberCollection[0].number);
}Or do I need to use a collection of the type
collection<*myData>
? -
Hello! My question is, do STL containers store a copy or they keep the object itself? Hence, can I insert a local variable into a container?
typedef struct{
int number;
string name; } myData;
class myClass{
void InsertData(myData &a);
void oneFunction ();
void otherFunction ();
collection m_memberCollection;
};
void oneFunction() {
myData localData;
localData.name = "Paul";
InsertData(localData);
}
void myClass::InsertData (myData &a){
a.number = 5;
m_memberCollection.push_back(a);
}
void otherFunction (){
printf ("%s %d", m_memberCollection[0].name.c_str(), m_memberCollection[0].number);
}Or do I need to use a collection of the type
collection<*myData>
?piul wrote:
Or do I need to use a collection of the type
collection<*myData>
?That would be a collection of pointers, which would probably lead to memory leaks and/or loss of information. Your original code takes copies of the objects and adds them to the collection, so the data is retained.
-
piul wrote:
Or do I need to use a collection of the type
collection<*myData>
?That would be a collection of pointers, which would probably lead to memory leaks and/or loss of information. Your original code takes copies of the objects and adds them to the collection, so the data is retained.
-
Hello! My question is, do STL containers store a copy or they keep the object itself? Hence, can I insert a local variable into a container?
typedef struct{
int number;
string name; } myData;
class myClass{
void InsertData(myData &a);
void oneFunction ();
void otherFunction ();
collection m_memberCollection;
};
void oneFunction() {
myData localData;
localData.name = "Paul";
InsertData(localData);
}
void myClass::InsertData (myData &a){
a.number = 5;
m_memberCollection.push_back(a);
}
void otherFunction (){
printf ("%s %d", m_memberCollection[0].name.c_str(), m_memberCollection[0].number);
}Or do I need to use a collection of the type
collection<*myData>
?Not particularly pertinent to the question but just a quick note... You don't need to write
typedef
struct
s in C++ the way you do in C to introduce a new type. Instead you can just write:struct myData
{
// Members
};and it'll do almost the same thing as your
typedef
(try adding some member functions to see what the difference is). -
Not particularly pertinent to the question but just a quick note... You don't need to write
typedef
struct
s in C++ the way you do in C to introduce a new type. Instead you can just write:struct myData
{
// Members
};and it'll do almost the same thing as your
typedef
(try adding some member functions to see what the difference is).I wonder how many people will catch that hint, so here goes: The only difference is that the typedef'd struct is unnamed, so you cannot add a constructor or destructor! You could fix that by adding a name explicitely:
typedef struct myData_struct {
...
} myData;But of course then you wouldn't need the typedef at all since
myData
would be synonymous tomyData_struct
. -
Not particularly pertinent to the question but just a quick note... You don't need to write
typedef
struct
s in C++ the way you do in C to introduce a new type. Instead you can just write:struct myData
{
// Members
};and it'll do almost the same thing as your
typedef
(try adding some member functions to see what the difference is).Absolutely right, but for those of us with K&R hardwired into the brain it's a tough habit to kick.