Saturday, March 31, 2007

class Foo {
public:
Foo() : bar_("foo_bar") {}

private:
const string bar_;
};


In this C++ code, you should not declare bar_ as
const string& because the initializer creates an instance
of string with "foo_bar" char* and it will be destroyed
immediately after it is out of scope if it is referenced.

No comments: