求教。c++primer里的动态内存一章
c++primer第十二章(第五版)的第一节里面编的那个StrBlob类到底是什么意思,搞不懂,求大神给用中国话解释解释。谢谢。我把程序抄出来了。#include<iostream>
#include<string>
#include<vector>
#include<memory>
#include<initializer_list>
class StrBlob
{
public:
typedef std::vector<std::string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<std::string> il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const std::string &t){ data->push_back(t); }
void pop_back();
std::string& front();
std::string& back();
private:
std::shared_ptr<std::vector<std::string> > data;
void check(size_type i, const std::string &msg) const;
};
using namespace std;
StrBlob::StrBlob() :data(make_shared<vector<string> >()){}
StrBlob::StrBlob(initializer_list<string> il):
data(make_shared<vector<string> >(il)){}
void StrBlob::check(size_type i, const string &msg)const
{
if (i >= data->size())
throw out_of_range(msg);
}
string& StrBlob::front()
{
check(0, "front on empty StrBlob");
return data->front();
}
string& StrBlob::back()
{
check(0, "back on empty StrBlob");
return data->back();
}
void StrBlob::pop_back()
{
check(0, "pop_back on empty StrBlob");
data->pop_back();
}