In my project, most objects are created in the arena, and it is guaranteed that they exist during the user's session. Therefore, for some class, it is enough to have a constant reference as a member field, for example:
class A {
public:
A(const string& str) : str_(str) {}
private:
const string& str_;
};
But there is a trap. By mistake, you can create an instance Aas follows:
A a("some temporal string object");
On this line, the temporary object stringwas implicitly created and destroyed. Therefore, after that it Astores the wrong link.
How to prevent this behavior? It would be better if this led to a compilation error ...
source
share