I have a C++ questions want to ask.
In the header, I defined a struct as following:
struct ObjectType
{
const int id;
const std::string& value;
ObjectType(const int id, const std::string& value = ""):
id (id)
, value (value)
{
}
};
In the anonymous namespace, I defined three variables:
namespace {
const ObjectType sample1 (0, "sample1");
const ObjectType sample2 (1, "sample2");
const ObjectType sample3 (2, "sample3");
}
Then in my unittest when I try to use the ObjectType, the value is always be NULL:
TEST(TestClass, test01)
{
std::cout << sample1.value << std::endl; // => This is always empty
}
As I remember since I defined the struct field as const, it should extend the input lifetime. Then sample1.value should be printed. However it doesn't.. Anyone knows why it happens? Thanks!