I need to create object pointers in a loop, but I am struggling with creating unique pointers. Here's my code:
class DatClass{
public:
int m;
DatClass(int i):m(i){}
};
class OtherClass{
public:
DatClass* dc;
};
void Test(std::vector<OtherClass> v){
std::vector<OtherClass>::iterator it;
int i = 1;
for(it = v.begin(); it != v.end(); it++){
DatClass dc = DatClass(i);
std::cout << &dc << std::endl;
it->dc = &dc;
i++;
}
}
int main(){
std::vector<OtherClass> v;
v.push_back(OtherClass());
v.push_back(OtherClass());
v.push_back(OtherClass());
Test(v);
}
This does not give me unique pointers. The output reads:
0xbf94d72c
0xbf94d72c
0xbf94d72c
Do I need to use new in order to get unique pointers? If so, where would I put the corresponding delete? Thanks!
DatClass dc;instead ofDatClass* dc;? That would get rid of both problems (how to get unique pointers and where to put delete).Test()doesn't even look at the vectorv, except to measure its length.