better illustrated by example:
tok.h
#include <string>
static const char* defaultDelim = ".,;";
class Tokenizer {
public:
Tokenizer():
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 incorrect. Real code gets seg error.
I would expect that here the rule of extending the life of the pace assigned to the const link will be respected, but apparently this is not so. Do you know the reason?
davka source
share