C++11智能指针之使用shared_ptr实现多态
来源:互联网 发布:vr照片拍摄软件 编辑:程序博客网 时间:2024/06/10 03:40
指针除了管理内存之外,在C++中还有一个重要的功能就是实现多态。
代码很简单,还是使用虚函数。与原生指针并没有什么区别:
#include <iostream>#include <memory>using namespace std;class parent{public: parent() { cout << "parent constructor" << endl; } virtual void showinfo() { cout << "parent info" << endl; } ~parent() { cout << "parent destructor" << endl; }};class child : public parent{public: child() { cout << "child constructor" << endl; } virtual void showinfo() { cout << "child info" << endl; } ~child() { cout << "child destructor" << endl; }};int main(){ shared_ptr<parent> sp = make_shared<child>(); sp->showinfo(); return 0;}
运行程序,输出为:
parent constructorchild constructorchild infochild destructorparent destructor
0 0
- C++11智能指针之使用shared_ptr实现多态
- C++:智能指针之shared_ptr
- C++11智能指针之shared_ptr的使用(1)
- 智能指针之shared_ptr
- 智能指针之 shared_ptr
- 智能指针之shared_ptr
- 智能指针之shared_ptr
- 智能指针之shared_ptr
- 智能指针之 shared_ptr
- 智能指针之shared_ptr
- 智能指针之shared_ptr
- C++11智能指针之shared_ptr、weak_ptr
- C++11智能指针之shared_ptr、weak_ptr
- C++ 11智能指针之shared_ptr
- C++ 11智能指针之shared_ptr
- C++ 11智能指针之shared_ptr
- C++ 11智能指针之shared_ptr
- C++11智能指针之std::shared_ptr
- 位运算与scanf
- 思科交换机IOS升级
- Git学习
- 自己写的各种可能会用到的东西
- HDU 4320 Arcane Numbers 1
- C++11智能指针之使用shared_ptr实现多态
- 决策树
- 第五周项目1-三角形类雏形(3)
- HTTPS原理
- 学习搭建Git服务器和多人协作开发
- ios9中的一些坑
- Android中的DexClassLoader使用
- unity所有特殊文件夹的用途
- Java中Linked中push(),pop()方法