C++primer第五版第13小节String类碰到的问题
来源:互联网 发布:雄猫软件 编辑:程序博客网 时间:2024/06/08 05:05
关于在VecStr类内设置static allocator变量,因为类内只能声明不能定义,所以需要在类外在声明一次编译才能通过,这是上一小节碰到的问题总结的。但是这次编译String类,按照VecStr为模板来改,但是再加上String(const String&);这样的构造函数后,再次使用类似String a(“”asd“”)这样的函数会报错。通过调试注释掉了//String() :element(nullptr), first_free(nullptr) {}加上了String() :String("") {}这样一个初始化的构造函数。通过调用String(const char*)中的reinitializer函数中的allocator中allocate来分配内存借此初始化类中的allocator类变量,然后主程序编译成功。
以下是String类
#include<memory>
#include<utility>class String {
public:
String() :String("") {}//初始化allocator通过String(const char*)构造函数中的reinitilizer函数进行初始化
//String() :element(nullptr), first_free(nullptr) {}
String(const String&);
String &operator=(const String&);
String(const char*);
~String();
size_t size() { return first_free - element; }
size_t length() { return first_free - element - 1; }
char *c_str()const { return element; }
bool empty() { return!size(); }
private:
std::pair<char*, char*> alloc_n_back(const char*, const char*);
void reinitializer(const char*, const char*);
void free();
private:
char *element;
char *first_free;
static std::allocator<char> alloc;
};
std::allocator<char> String::alloc;
std::pair<char*, char*> String::alloc_n_back(const char *b, const char *e)
{
auto data = alloc.allocate(e - b);
return{ data, std::uninitialized_copy(b, e, data) };
}
void String::reinitializer(const char *first, const char *last)
{
auto newdata = alloc_n_back(first, last);
element = newdata.first;
first_free = newdata.second;
}
String::String(const char *s)
{
char *s_1 = const_cast<char*>(s);
while (*s_1++);
reinitializer(s, s_1);
}
String::String(const String &s_1)
{
reinitializer(s_1.element, s_1.first_free);
std::cout << "as" << std::endl;
}
void String::free()
{
if (element)
{
std::for_each(element, first_free, [](char &a) {alloc.destroy(&a); });//lambda里的&引用符号一定不能忘记
alloc.deallocate(element,first_free-element);
}
}
String::~String()
{
free();
}
String &String::operator=(const String &s_1)
{
auto data = alloc_n_back(s_1.element, s_1.first_free);
free();
element = data.first;
first_free = data.second;
std::cout << "cp-as" << std::endl;
return *this;
}
0 0
- C++primer第五版第13小节String类碰到的问题
- c++ primer第五版(中文)习题答案 第十章第三节第二小节-lambda表达式
- c++ primer第五版(中文)习题答案 第十章第三节第四小节-参数绑定
- c++ primer第五版(中文)习题答案 第十章第五节第一小节-5类迭代器
- C Primer Plus(第五版)中文版的问题
- C++primer第五版书店问题
- c++ primer第五版(中文)习题答案 第十章第三节第一小节-向算法传递函数
- c++ primer第五版(中文)习题答案 第十章第三节第三小节-lambda捕获和返回
- c++ primer第五版(中文)习题答案 第十章第五节第二三小节-算法形参合命名规范
- c++primer 第五版
- 《C Primer Plus(第五版)中文版》第13章第1至13题
- 《C Primer Plus(第五版)中文版》第10章第1至13题
- C++Primer 第五版 3.类设计者的工具
- C Primer Plus(第6版)第五章答案
- C++Primer第五版 第7章 类(练习解答)
- c++ primer第五版(中文)习题答案 第十章第二节第二小节-写容器元素的算法
- c++ primer第五版(中文)习题答案 第十章第二节第三小节-重排容器元素的算法
- C Primer Plus 第五版
- Mac上常用命令
- Maven用法
- http://fx114.net/qa-255-158546.aspx
- 第十二周项目4-利用遍历思想求解图问题(6)
- 第十三周项目1 Prim算法的验证
- C++primer第五版第13小节String类碰到的问题
- java--分页技术(1)
- java 接受 json 数据
- Java和C#环境中制作二维码图片
- unity3D-游戏/AR/VR在线就业班 C#入门类和对象学习笔记
- Android TextView 文字居中
- insmod: short read错误
- Cesium 隐藏三维地球版本信息
- c语言中赋值截断