Question says it all really.
Would it be better to use const pointer members instead if I were to implement the default constructor?
You need to use the member initialization list:
struct foo
{
const int& ref;
foo() : ref(some_value()) { }
}
Make sure some_value()
doesn't give you a temporary. It will only have its life extended until the end of the constructor:
A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With