Moving a STL container around
-
A STL container is not your typical c++ class, how do you declare a vector pointer and pass it to a function.
class someclass
{
public:
int ID;
}
void somefunction(vector* vec)
{
someclass scitem;vec->push_back(scitem);
}
vector* vec;
somefunction(vec);I`m also not sure how a vector of pointers would work.
void somefunction(vector* vec)
{
someclass * scitem;
scitem = (someclass*)malloc(sizeof(someclass));vec->push_back(scitem);
}
vector* vec2;
somefuntion(vec2); -
A STL container is not your typical c++ class, how do you declare a vector pointer and pass it to a function.
class someclass
{
public:
int ID;
}
void somefunction(vector* vec)
{
someclass scitem;vec->push_back(scitem);
}
vector* vec;
somefunction(vec);I`m also not sure how a vector of pointers would work.
void somefunction(vector* vec)
{
someclass * scitem;
scitem = (someclass*)malloc(sizeof(someclass));vec->push_back(scitem);
}
vector* vec2;
somefuntion(vec2);Yes, STL containers are just template classes and can be manipulated the same as any class objects. And as such, a vector may contain objects, or pointers to objects. But remember in the second case that the items pointed to, must not be removed while the vector still owns them.
std::vector* pMyvec = new std::vector(); // a pointer to a vector of pointers.
// use push_back to fill the vector with pointers:
pMyvec->push_back("A string");
pMyvec->push_back("Another string");// pass the vectro to a function:
int result = myfun(pMyvec);
// on return the contents may have been changedBTW you should not use
malloc
in C++ code, asnew
anddelete
is the correct way to manage memory. -
Yes, STL containers are just template classes and can be manipulated the same as any class objects. And as such, a vector may contain objects, or pointers to objects. But remember in the second case that the items pointed to, must not be removed while the vector still owns them.
std::vector* pMyvec = new std::vector(); // a pointer to a vector of pointers.
// use push_back to fill the vector with pointers:
pMyvec->push_back("A string");
pMyvec->push_back("Another string");// pass the vectro to a function:
int result = myfun(pMyvec);
// on return the contents may have been changedBTW you should not use
malloc
in C++ code, asnew
anddelete
is the correct way to manage memory.Quote:
And as such, a vector may contain objects, or pointers to objects
But when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added. A linked list deals with pointers not objects.
-
Quote:
And as such, a vector may contain objects, or pointers to objects
But when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added. A linked list deals with pointers not objects.
If you add an object to an STL container (
vector
,list
,set
,map
...), the object is copied into the container. The objects must all be of the same type, however. The only way to put polymorphic objects into a container is to use a container of pointers to their common base class, in which case the objects need to survive outside the container, probably by having been allocated from the heap.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
If you add an object to an STL container (
vector
,list
,set
,map
...), the object is copied into the container. The objects must all be of the same type, however. The only way to put polymorphic objects into a container is to use a container of pointers to their common base class, in which case the objects need to survive outside the container, probably by having been allocated from the heap.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.thanks Greg, I`ll keep that in mind.
-
Quote:
And as such, a vector may contain objects, or pointers to objects
But when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added. A linked list deals with pointers not objects.
CalinNegru wrote:
when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added.
No, the vector copies whatever type you have declared it with. For example:
std::vector myVec;
std::string newstr = "A string";
myVec.push_back(newstr); // myVec now contains a copy of the string, not a pointer.CalinNegru wrote:
A linked list deals with pointers not objects.
Well sometimes it does; but what has that to do with this question?
-
CalinNegru wrote:
when you add a class object, the vector doesn`t make a copy of the object, it creates a pointer to the object being added.
No, the vector copies whatever type you have declared it with. For example:
std::vector myVec;
std::string newstr = "A string";
myVec.push_back(newstr); // myVec now contains a copy of the string, not a pointer.CalinNegru wrote:
A linked list deals with pointers not objects.
Well sometimes it does; but what has that to do with this question?
ok, I think I get your point