如何实现 Copying derived entities using only base class pointer
#include <iostream> struct CloneableBase {
virtual CloneableBase* clone() const = ;
}; template<class Derived>
struct Cloneable : CloneableBase {
virtual CloneableBase* clone() const {
return new Derived(static_cast<const Derived&>(*this));
}
}; struct D1 : Cloneable<D1> {
D1() {}
D1(const D1& other) {
std::cout << "Copy constructing D1\n";
}
}; struct D2 : Cloneable<D2> {
D2() {}
D2(const D2& other) {
std::cout << "Copy constructing D2\n";
}
}; int main() {
CloneableBase* a = new D1();
CloneableBase* b = a->clone();
CloneableBase* c = new D2();
CloneableBase* d = c->clone();
}
http://stackoverflow.com/questions/5027456/copying-derived-entities-using-only-base-class-pointers-without-exhaustive-tes
static_cast<const Derived&> 注意这里,因为函数的参数就是引用,所以这里转换成引用
如何实现 Copying derived entities using only base class pointer的更多相关文章
- 【转载】#335 - Accessing a Derived Class Using a Base Class Variable
You can use a variable whose type is a base class to reference instances of a derived class. However ...
- 论文《A Generative Entity-Mention Model for Linking Entities with Knowledge Base》
A Generative Entity-Mention Model for Linking Entities with Knowledge Base 一.主要方法 提出了一种生成概率模型,叫做en ...
- A Base Class pointer can point to a derived class object. Why is the vice-versa not true?
问题转载自:https://stackoverflow.com/questions/4937180/a-base-class-pointer-can-point-to-a-derived-class- ...
- Programming Entity Framework 翻译(1)-目录
1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...
- C++ 虚函数机制学习
致谢 本文是基于对<Inside the c++ object model>的阅读和gdb的使用而完成的.在此感谢Lippman对cfront中对象模型的解析,这些解析帮助读者拨开迷雾.此 ...
- [C++] OOP - Base and Derived Classes
There is a base class at the root of the hierarchy, from which the other class inherit, directly or ...
- Inheritance: 'A' is an inaccessible base of 'B'
'boost::enable_shared_from_this<net::Session>' is an inaccessible base of 'net::Session' BOOST ...
- How to use base class's assignment operator in C++
看了android下的代码,好长时间没看了,有个关于C++的知识点不是很清楚,查了下: 如何使用基类中的赋值运算符? 引述自http://stackoverflow.com/questions/122 ...
- [C++] OOP - Virtual Functions and Abstract Base Classes
Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...
随机推荐
- Flask实战第59天:首页帖子布局完成
编辑front_index.html <div id="carousel-example-generic" class="carousel slide index- ...
- 安卓 应用app启动过程
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 从用户点击 Launcher 上的 App 图标,到显示出 App 界面时主要发生的事情.知晓以 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- [P4064][JXOI2017]加法(贪心+树状数组+堆)
题目描述 可怜有一个长度为 n 的正整数序列 A,但是她觉得 A 中的数字太小了,这让她很不开心. 于是她选择了 m 个区间 [li, ri] 和两个正整数 a, k.她打算从这 m 个区间里选出恰好 ...
- 搭建vsftpd
安装完软件以后 1.建立用户 adduser ftp passwd 密码 2.修改vsftp.conf文件 anonymous_enable改为NO,阻止匿名上传 Anon_mkdir_write_e ...
- Android网络通信框架LiteHttp2.0 开篇简介和大纲目录
本帖最后由 移动天宇 于 2015-10-26 10:42 编辑 LiteHttp2.0很多东东焕然一新,旧的能力也得到增强,没有使用的同学来了解一下吧. Android网络框架为什么可以选用lite ...
- Chrome 控制台新玩法-向输出到console的文字加样式
Chrome 控制台新玩法-向输出到console的文字加样式 有兴趣的同学可以文章最后的代码复制贴到控制台玩玩. Go for Code 在正常模式下,一般只能向console 控制台输出简单的文字 ...
- centOS6.5 Hadoop1.0.4安装
前段时间去培训,按照教程装了一遍Hadoop.回来又重新装一次,捋下思路,加深理解. 基本配置如下,三个节点,一个namenode,两个datanode. Namenode 192.168.59.14 ...
- 如何使用SQLMAP绕过WAF
WAF(web应用防火墙)逐渐成为安全解决方案的标配之一.正因为有了它,许多公司甚至已经不在意web应用的漏洞.遗憾的是,并不是所有的waf都是不可绕过的!本文将向大家讲述,如何使用注入神器SQLMa ...
- Screen多视窗远程控制管理服务
Screen是一款由GNU开源计划开发的多视窗远程控制管理服务,简单来说就是为了解决上述情况中网络异常中断或同时控制多个远程窗口而设计的程序. Screen服务程序不仅能够解决上述问题,而且用户在使用 ...