我需要一个shared_ptr类似的对象,但在我尝试访问其成员时会自动创建一个真实的对象.
例如,我有:
class Box
{
public:
unsigned int width;
unsigned int height;
Box(): width(50), height(100){}
};
std::vector< lazy<Box> > boxes;
boxes.resize(100);
// at this point boxes contain no any real Box object.
// But when I try to access box number 50, for example,
// it will be created.
std::cout << boxes[49].width;
// now vector contains one real box and 99 lazy boxes.
Run Code Online (Sandbox Code Playgroud)
是否有一些实现,或者我应该自己编写?