Inheritance is the base class of evil (Sean Parent)
来源:互联网 发布:1光年等于多少米的算法 编辑:程序博客网 时间:2024/05/20 06:26
#include <vector>
#include <string>
#include <memory>
#include <cassert>
using namespace std;
template<typename T>
void draw(const T& x, ostream& out, size_t pos) {
out << string(pos, ' ') << x << "\n";
}
class object_t {
public:
template<typename T>
object_t(T x) : self_(make_shared<model<T>>(move(x))) {
cout << "ctor\n";
}
friend void draw(const object_t& x, ostream& out, size_t pos) {
x.self_->draw_(out, pos);
}
private:
struct concept_t {
virtual ~concept_t() = default;
virtual void draw_(ostream&, size_t) const = 0;
};
template<typename T>
struct model: concept_t {
model(T x) : data_(move(x)) {
}
void draw_(ostream& out, size_t pos) const {
draw(data_, out, pos);
}
T data_;
};
private:
shared_ptr<const concept_t> self_;
};
using document_t = vector<object_t>;
class my_class_t {
};
void draw(const my_class_t&, ostream& out, size_t pos) {
out << string(pos, ' ') << "my_class_t\n";
}
void draw(const document_t& x, ostream& out, size_t pos) {
out << string(pos, ' ') << "<document>\n";
for (const auto& e: x) {
draw(e, out, pos + 2);
}
out << string(pos, ' ') << "</document>\n";
}
using history_t = vector<document_t>;
void commit(history_t& x) {
assert(!x.empty());
x.push_back(x.back());
}
void undo(history_t& x) {
assert(!x.empty());
x.pop_back();
}
document_t& current(history_t& x) {
assert(!x.empty());
return x.back();
}
int main() {
history_t h(1);
current(h).emplace_back(0);
current(h).emplace_back(string("hello"));
draw(current(h), cout, 0);
cout << "-----------------------\n";
commit(h);
current(h).emplace_back(current(h));
current(h).emplace_back(my_class_t());
cout << "change hello\n";
current(h)[1] = string("world");
draw(current(h), cout, 0);
cout << "-----------------------\n";
undo(h);
draw(current(h), cout, 0);
return 0;
}
- Inheritance is the base class of evil (Sean Parent)
- Inheritance: 'A' is an inaccessible base of 'B'
- Premature optimization is the root of all evil.
- The base class includes the field '...', but its type (...) is not compatible with the type of control (...)
- the memory of virtual base class of C++
- perl---inheritance of one class
- The Inheritance Hierarchy of QT
- whether the results are the same of getClass().getName() called in child class and parent class?
- Gym 101028G-The Tower of Evil
- 【填坑】The hierarchy of the type class is inconsistent
- Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "te
- specified class size for type `CatType' is smaller than the parent type's `GObject
- 95. Caution is the parent of safety. 小心驶得万年船
- 231. Industry is the parent of success. 勤奋是成功之母
- Why is the size of an empty class not zero?
- Why is the size of an empty class not zero?
- Why is the size of an empty class not zero?
- 18 What is the __dict__.__dict__attribute of a Python class
- 5 循环和关系表达式
- java图的遍历方式(深度遍历、广度遍历)
- 开源站点
- Win7 disk.sys无法加载的问题
- WinCE7的文件系统注册表设置
- Inheritance is the base class of evil (Sean Parent)
- Hacking Windows CE!Windows Embedded Compact 7 文件系统概要
- java实现汉诺塔
- wince bsp中的g_oalAddressTable表
- Cocos2d-x 3.0 开发(九)使用Physicals代替Box2D和chipmunk
- 法国龙凤胎洗澡睡着 紧抱对方相互依偎(图)
- 基于tomcat+mysql的c/s模式下的系统自动更新
- (1)使用GridView和Fragment实现Tab效果
- tar 压缩与解压缩打包命令