#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. 如何使用Web字体?

    如何使用Web字体 嵌入Web字体的关键是@font-face规则,通过它可以指定浏览器下载web字体的地址,以及如何在样式表中引用该字体 @font-face { font-family: Voll ...

  2. Linux基础系列-Day3

    Vim文本编辑器 •Linux设计的重要原则是信息存储在基于文本的文件中.  注:Linux“一切皆文件”是指包含文本文件和用户不可读的二进制文件(如block设备文件) •文本文件:无格式文件,作用 ...

  3. 【HDU 5283】Senior's Fish

    http://acm.hdu.edu.cn/showproblem.php?pid=5283 今天的互测题,又爆零了qwq 考虑每个点对答案的贡献. 对每个点能产生贡献的时间线上的左右端点整体二分. ...

  4. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses

    [题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...

  5. 【分块】【bitset】hdu6085 Rikka with Candies

    给你数组A和B,A B中的元素大小都不超过5w,且两两不同. q次询问,每次给你个k,问你有多少对(i,j),满足A(i)%B(j)==k. 如题目所言模拟bitset的过程,实质上是个分块,每块的大 ...

  6. Problem D: 调用函数,输出Fibonacci数列的m项至n项

    #include<stdio.h> int fib(int n)//定义FIbonacci函数 { int s,i; ||n==) { s=; } else { int s1,s2; s1 ...

  7. ES6中的一些新特性

    这两个命令是ES6的新语法知识.这两个新的特性解决了ES6中的一些小的"bug"问题.其中包含一些知识:块级作用域.let命令.const命令.全局对象的属性.Google V8引 ...

  8. bootstrap学习(全局CSS样式)(二)

    标题类:.h1到.h6 页面主体 bootstrap将全局font-size设置为14px,line-height设置为1.428,这些属性 直接赋予元素和所有段落元素. 文本对齐类 text-lef ...

  9. [转]Java transient关键字

    java 的transient关键字的作用是需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中. trans ...

  10. Educational Codeforces Round 9 E. Thief in a Shop dp fft

    E. Thief in a Shop 题目连接: http://www.codeforces.com/contest/632/problem/E Description A thief made hi ...