better explained by an example:
tok.h
#include <string> static const char* defaultDelim = ".,;"; class Tokenizer { public: Tokenizer(): // 'delim' is the const ref member that is initialized by the temp string delim( (altDelim.size())? altDelim : std::string(defaultDelim) ) {} size_t scan(const std::string& str) { return str.find_first_of(delim); } static void setDelim(const std::string& d) { altDelim = d; } private: static std::string altDelim; const std::string& delim; }; main.cpp
#include <iostream> using namespace std; #include "tok.h" std::string Tokenizer::altDelim; int main() { Tokenizer tok; size_t pos = tok.scan("hello, world"); cout << pos << endl; } the program prints 0 which is wrong. The real code gets a seg fault.
I'd expect that the rule of prolonging the life span of a temp assigned to a const reference would hold here, but apparently it's not. Do you know the reason'?