N Strahl
N Strahl

Reputation: 85

using a range-based for-loop to iterate through a list of pointers

Good day everyone,

I have been trying the following code to reset all pointers in a list to nullptr as shown below:

    for (auto item : { ptr1, ptr2, ptr3 })
        item = nullptr;

the problem is that a copy of the pointer is made to item, so the original ptrs are unequal to nullptr at the end.

I would like to get some sort of reference to the pointers themselves or maybe there is already a better alternative to setting all pointers in a container to nullptr. If any of you happen to work with Qt maybe there's a good way of doing it in Qt? Thanks in advance!

Upvotes: 2

Views: 1390

Answers (2)

dfrib
dfrib

Reputation: 73176

If you are typing the individual pointer variables out, why not just use a chained assignment?

ptr1 = ptr2 = ptr3 = nullptr;  // All three pointers are nullptr.

DEMO.


[...] or maybe there is already a better alternative to setting all pointers in a container to nullptr.

If you have your pointers in an actual containers as compared to separate, named pointer variables, you can use a range-based for loop to write to and read the pointer elements of the container:

#include <array>
#include <ios>
#include <iostream>

// Container of 'pointer-to-const-int' elements.
using MyPointersArray = std::array<int const*, 3>;

void inspectNullptrness(const MyPointersArray& arr) {
    for (const auto& p : arr) {
        std::cout << std::boolalpha 
            << (p == nullptr) << " ";
    }
}

int main() {
    MyPointersArray pointers{nullptr, nullptr, nullptr};
    const int a = 42;
    inspectNullptrness(pointers); // true true true
    
    // Set pointers.
    for(auto& p : pointers) { p = &a; }
    inspectNullptrness(pointers); // false false false
    
    // Reset pointers.
    for(auto& p : pointers) { p = nullptr; }
    inspectNullptrness(pointers); // true true true
}

Upvotes: 4

eerorika
eerorika

Reputation: 238311

I would like to get some sort of reference

You're creating a std::initializer_list with that braced init list. Unfortunately, such list always contains copies and you cannot assign to the variables used to initialise it through that list.

You could use reference wrappers:

for (auto ref : { std::ref(ptr1), std::ref(ptr2), std::ref(ptr3) })
    ref.get() = nullptr;

If you used an array of pointers instead of separate variables, then iterating over them would be trivial:

T* ptrs[3];
for (auto& ptr : ptrs})
    ptr = nullptr;

Upvotes: 2

Related Questions