how do I convert a reference to a pointer correctly?
The code below gives me the following warning: “taking address of temporary”.
MyClass myclass;
vector<MyClass*> myClassList;
myClassList.push_back(&myclass);
MethodThatsNeedsVectorOFMyClassPointers(myClassList);
The way your code looks like now, you shouldn’t get a warning since
myclassandmyClassListhave the same lifetime. However, ifmyClassListoutlivesmyclass, you need to dynamically allocateMyClass:If the following is closer to what you actually have:
then
myclassis destroyed at the closing}, andmyClassListwill contain a pointer to released memory.Also, is
MyClasspolymorphic? Do you really need to store pointers in the vector?