如何实现 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 ...
随机推荐
- c++风格
http://web.archive.org/web/20160430022340/http://google.github.io/styleguide/cppguide.html 主要注意几点: 函 ...
- 【数形结合】Erratic Expansion
[UVa12627]Erratic Expansion 算法入门经典第8章8-12(P245) 题目大意:起初有一个红球,每一次红球会分成三红一蓝,蓝球会分成四蓝(如图顺序),问K时的时候A~B行中有 ...
- Problem C: 程序改错(递归函数):数字转字符
Description 下面程序中“/ ***** N ***** /”的下一行中有错误,请改正(注意:不得加行.减行.加句.减句,否则后果自负). 该程序功能:用递归法将一个六位整数n转换成字符串, ...
- [转]json+JSONObject+JSONArray 结合使用
JSONObject与JSONArray的区别简述: 区别在于JSONObject是一个{}包裹起来的一个对象(Object),而JSONArray则是[]包裹起来的一个数组(Array),说白点就是 ...
- Delphi Xe10
http://blog.csdn.net/tht2009/article/details/48165371
- jQuery中的Ajax全局事件
Ajax全局事件 全局事件会在有ajax请求的情况下触发. 方法名称 说明 ajaxStart(callback) Ajax请求开始时执行的函数 ajaxStop(callback) Ajax请求结束 ...
- 详解MySQL性能优化(二)
http://www.jb51.net/article/70530.htm 七.MySQL数据库Schema设计的性能优化高效的模型设计 适度冗余-让Query尽两减少Join 大字段垂直分拆-sum ...
- Android进阶笔记:AIDL内部实现详解 (一)
AIDL内部实现详解 (一) AIDL的作用是实现跨进程通讯使用方法也非常的简单,他的设计模式是典型的C/S架构.使用AIDL只要在Client端和Server端的项目根目录下面创建一个aidl的文件 ...
- List of regional organizations by population
https://baike.baidu.com/item/国际组织/261053?fr=aladdin 经济文化类组织(非政府组织) 创行 狮子会 全球青年领导力联盟 乐施会 政治类组织 欧洲联盟(欧 ...
- jquery ajax 不执行赋值,return没有返回值的解决方法
大家先看一段简单的jquery ajax 返回值的js 复制代码 代码如下: function getReturnAjax{ $.ajax({ type:"POST", url:& ...