#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的更多相关文章

  1. 【转载】#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 ...

  2. 论文《A Generative Entity-Mention Model for Linking Entities with Knowledge Base》

    A Generative Entity-Mention Model for Linking Entities with Knowledge Base   一.主要方法 提出了一种生成概率模型,叫做en ...

  3. 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- ...

  4. Programming Entity Framework 翻译(1)-目录

    1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...

  5. C++ 虚函数机制学习

    致谢 本文是基于对<Inside the c++ object model>的阅读和gdb的使用而完成的.在此感谢Lippman对cfront中对象模型的解析,这些解析帮助读者拨开迷雾.此 ...

  6. [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 ...

  7. Inheritance: 'A' is an inaccessible base of 'B'

    'boost::enable_shared_from_this<net::Session>' is an inaccessible base of 'net::Session' BOOST ...

  8. How to use base class's assignment operator in C++

    看了android下的代码,好长时间没看了,有个关于C++的知识点不是很清楚,查了下: 如何使用基类中的赋值运算符? 引述自http://stackoverflow.com/questions/122 ...

  9. [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 ...

随机推荐

  1. Maven环境配置及idea建Maven工程

    https://blog.csdn.net/qq_37497322/article/details/78988378

  2. codevs 1349 板猪的火车票

    1349 板猪的火车票  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 奸商zn(请勿对号入座)开办了一家火车公司,弱弱的板猪 ...

  3. 一段javascript设计模式应用场景

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  4. mysql设置远程访问密码

    mysql -u root -p Aaa111222 grant all privileges on *.* to root@'%' identified by 'aaa111222; Quit ln ...

  5. SpringMVC中拦截器的使用

    什么是拦截器 拦截器通常一般指的是通过拦截从浏览器发往服务器的一些请求来完成某些功能的一段程序代码一般在一个请求发生之前,发生时,发生后我们都可以对请求进行拦截 拦截器可以做什么 拦截器可以用于权限验 ...

  6. 如何解决sqlmapapi重启后,任务全部丢失的问题

    sqlmapapi的server每次启动时都会创建一个新的数据库,这样之前的扫描记录都会全部丢失 使用python sqlmapapi.py -s可以看大IPC database的位置,这个各个操作系 ...

  7. 事务内执行sql修复的简易模板

    -- ============================================= -- Script Template -- ============================= ...

  8. 用 Chrome 扩展实现修改

      用 Chrome 扩展实现修改 ajax 请求的响应 wincss 4 个月前 背景 Fiddler 和 Charles 是常见的 HTTP 调试器,它们会在本地运行一个代理服务器,可以查看浏览器 ...

  9. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  10. [视频解说]Java(JDK的下载安装及第一个程序执行)

    (JDK的下载安装及第一个程序执行) 内容:Java JDK 的安装以及HelloWorld 程序的执行 欢迎童鞋们前往围观 http://v.youku.com/v_show/id_XODA3Mzk ...