I am storing different objects which inherit from the same interface inside a single vector. Those objects have their name stored as a constant string view, which gets its value even before the object gets instantiated. I am able to easily retrieve its name before it gets stored in a vector. However, when I store it inside a vector of interfaces and I access its name again, I get undefined behavior (compiler-dependent). How can I fix this?
Here's the simplest case which demonstrates my problem. I'm using a compiler which supports C++17 and some features of C++20
#include <string_view>
#include <vector>
#include <iostream>
struct IFoo {
public:
virtual ~IFoo() = default;
const std::string_view name{};
};
struct Foo : public IFoo {
public:
const std::string_view name = "bar";
};
int main() {
std::vector<IFoo*> foos;
Foo *foo = new Foo();
std::cout << "Name of object before storing: " << foo->name << std::endl;
foos.push_back(new Foo());
for (auto foo : foos) {
std::cout << "Name of object after storing: " << foo->name << std::endl;
}
}
It produces the following results:
Name of object before storing: bar
Name of object after storing:
When I tried changing the vector to std::vector<Foo*>
, the code ran without any problems. However, this is not a solution, because I want to store multiple types which inherit from the same interface inside a single vector.