C++ constant reference lifetime
- by aaa
hello
I have code that looks like this:
class T {};
class container {
const T &first, T &second;
container(const T&first, const T & second);
};
class adapter : T {};
container(adapter(), adapter());
I thought lifetime of constant reference would be lifetime of container.
However, it appears otherwise, adapter object is destroyed after container is created, leading dangling reference.
What is the correct lifetime?
how to correctly implement binding temporary object to class member reference?
Thanks