Make shift list to modify pointed to objects
-
@fcarney Hah, right, I took too fast of a glance. Well, there's your answer. It doesn't help readability ;)
@Chris-Kawa I changed the code to be explicit ** in my code. auto is too confusing.
-
I originally did the code this way:
int* ptr1 = new int; int* ptr2 = new int; for(auto ptr: {ptr1, ptr2}){ if(ptr){ // call something on int* delete ptr; ptr = nullptr; } }But this modifies the copy of the pointer in the structure. Not the original pointer. This is a bug to write it this way.
Work on the reference:
@fcarney said in Make shift list to modify pointed to objects:
for(auto &ptr: {ptr1, ptr2}){ -
@Christian-Ehrlicher said in Make shift list to modify pointed to objects:
Work on the reference:
That wont work if I am trying to null the original pointer. The pointer in the make shift object (struct?) is a copy.
-
@Christian-Ehrlicher said in Make shift list to modify pointed to objects:
Work on the reference:
That wont work if I am trying to null the original pointer. The pointer in the make shift object (struct?) is a copy.
@fcarney That's 0:2 in the game of readability vs this code :)
-
@Christian-Ehrlicher said in Make shift list to modify pointed to objects:
Work on the reference:
That wont work if I am trying to null the original pointer. The pointer in the make shift object (struct?) is a copy.
-
@fcarney you know this is also an option:
int* ptr1 = new int; int* ptr2 = new int; auto doStuffWith = [](int *ptr){ if(ptr){ // call something on int* delete ptr; ptr = nullptr; } }; doStuffWith(ptr1); doStuffWith(ptr2);@J-Hilk said in Make shift list to modify pointed to objects:
auto doStuffWith = [](int *ptr){
This is still a copy of the pointer. It will work if I use a reference though:
auto doStuffWith = [](int* &ptr){Long run though, I should probably start using smart pointers:
std::shared_ptr<int> ptra(new int); std::shared_ptr<int> ptrb(new int); for(auto ptr: { &ptra, &ptrb }){ // do stuff ptr->reset(); } Q_ASSERT(ptra == nullptr); // for qobject QPointer<QObject> ptrc = new QObject(); QPointer<QObject> ptrd = new QObject(); for(auto& ptr: { ptrc, ptrd }){ // working with copy of qpointer, but meh // do stuff delete ptr; } Q_ASSERT(ptrc == nullptr); -
@J-Hilk said in Make shift list to modify pointed to objects:
auto doStuffWith = [](int *ptr){
This is still a copy of the pointer. It will work if I use a reference though:
auto doStuffWith = [](int* &ptr){Long run though, I should probably start using smart pointers:
std::shared_ptr<int> ptra(new int); std::shared_ptr<int> ptrb(new int); for(auto ptr: { &ptra, &ptrb }){ // do stuff ptr->reset(); } Q_ASSERT(ptra == nullptr); // for qobject QPointer<QObject> ptrc = new QObject(); QPointer<QObject> ptrd = new QObject(); for(auto& ptr: { ptrc, ptrd }){ // working with copy of qpointer, but meh // do stuff delete ptr; } Q_ASSERT(ptrc == nullptr);@fcarney said in Make shift list to modify pointed to objects:
for(auto& ptr: { ptrc, ptrd }){ // working with copy of qpointer, but meh
Nothing is copied here...
-
@fcarney said in Make shift list to modify pointed to objects:
for(auto& ptr: { ptrc, ptrd }){ // working with copy of qpointer, but meh
Nothing is copied here...
{ ptrc, ptrd }That was a crux of the original problem. This structure or whatever it is, has a copy of ptrc and ptrd objects.
When the objects were just pointers it was copies of the pointers. That is why setting to null only worked on the copy itself.
-
Oh interesting. The braces with objects create an std::initializer_list. That is why it works as a container with range for loops.
-
I think this is an example when you should use the STL containers. I frequently create containers of pointers of objects (handles) and iterate over them, but unless the container contains smart pointers you must remember to explicitly delete the objects.