相关疑难解决方法(0)

有C++懒惰指针吗?

我需要一个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)

是否有一些实现,或者我应该自己编写?

c++ pointers

14
推荐指数
2
解决办法
3865
查看次数

标签 统计

c++ ×1

pointers ×1