The code listed below triggers a segmentation fault in the iterator based loop:
#include <iostream>
#include <vector>
class A {
public:
A(unsigned id = 0) {id_ = id;}
unsigned get_id() {return id_;}
private:
unsigned id_;
};
class B {
public:
B() {}
B(std::vector<A*> entries) : entries_(entries) {}
const std::vector<A*> get_entries() const {
return entries_;
}
private:
std::vector<A*> entries_;
};
int main() {
std::vector<A*> entries;
for (unsigned i = 0; i < 5; i++) {
entries.push_back(new A(i));
}
B b(entries);
// index based access (ok)
for (unsigned i = 0; i < b.get_entries().size(); i++) {
std::cout << b.get_entries()[i]->get_id() << std::endl;
}
// iterator based access (segmentation fault)
for (std::vector<A*>::const_iterator i = b.get_entries().begin();
i != b.get_entries().end();
++i) {
std::cout << (*i)->get_id() << std::endl;
}
}
On the other hand, the index based loop works ok.
This behaviour is triggered when a copy of the std::vector
is returned (see: const std::vector<A*> get_entries() const
) and not a const
reference to it, as e.g. const std::vector<A*>& get_entries() const
.
The latter case works fine.
How could this behaviour be explained?