【发布时间】:2009-05-18 15:00:38
【问题描述】:
我需要一个类似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.
是否有一些实现,或者我应该自己编写?
【问题讨论】: