Effective C++ :模板类之间的继承.
2015-12-12 00:00
381 查看
#include <iostream> template<typename T> class ContainerBase { private: T baseData; public: ContainerBase(const T& data_); ContainerBase(T&& data_); ContainerBase(const ContainerBase<T>& other); ContainerBase(ContainerBase&& other); ContainerBase& operator=(const ContainerBase& other); ContainerBase& operator=(ContainerBase&& other); void print(); virtual ~ContainerBase() = default; }; template<typename T> ContainerBase<T>::ContainerBase(const T& data_) :baseData(data_) { std::cout << "const T&" << std::endl; } template<typename T> ContainerBase<T>::ContainerBase(T&& data_) :baseData(data_) { std::cout << "T&&" << std::endl; } template<typename T> ContainerBase<T>::ContainerBase(const ContainerBase<T>& other) :baseData(other.baseData) { std::cout << "copy-constructor" << std::endl; } template<typename T> ContainerBase<T>::ContainerBase(ContainerBase<T>&& other) :baseData(std::move(baseData)) { std::cout << "move-constructor" << std::endl; } template<typename T> ContainerBase<T>& ContainerBase<T>::operator=(const ContainerBase<T>& other) { std::cout << "operator=(const ContainerBase<T>&)" << std::endl; this->baseData = other.baseData; return *this; } template<typename T> ContainerBase<T>& ContainerBase<T>::operator=(ContainerBase<T>&& other) { std::cout << "operator=(Container<T>&& other)" << std::endl; this->baseData = std::move(other.baseData); return *this; } template<typename T> void ContainerBase<T>::print() { std::cout << "ContainerBase: " << this->baseData << std::endl; } template<typename Ty> class Container : public ContainerBase<Ty> { private: Ty data; public: using ContainerBase::ContainerBase; //声明使用派生类中的构造函数. Container(const Ty& dataOne, const Ty& dataTwo); Container(Ty&& dataOne, Ty&& dataTwo); Container(const Container<Ty>& other); Container(Container<Ty>&& other); Container<Ty>& operator=(const Container<Ty>& other); Container<Ty>& operator=(Container<Ty>&& other); virtual ~Container() = default; }; template<typename Ty> Container<Ty>::Container(const Ty& dataOne, const Ty& dataTwo) :ContainerBase(dataOne), data(dataBase) { std::cout << "Container Constructor const Ty&" << std::endl; } template<typename Ty> Container<Ty>::Container(Ty&& dataOne, Ty&& dataTwo) :ContainerBase(dataOne), data(dataTwo) { std::cout << "Container Constructor Ty&&" << std::endl; } template<typename Ty> Container<Ty>::Container(const Container<Ty>& other) :ContainerBase(other) { std::cout << "copy-constructor" << std::endl; this->data = other.data; } template<typename Ty> Container<Ty>::Container(Container<Ty>&& other) :ContainerBase(other), data(std::move(other.data)) { std::cout << "move-constructor" << std::endl; } template<typename Ty> Container<Ty>& Container<Ty>::operator=(const Container<Ty>& other) { ContainerBase<Ty>::operator=(other); this->data = other.data; return *this; } template<typename Ty> Container<Ty>& Container<Ty>::operator=(Container<Ty>&& other) { ContainerBase<Ty>::operator=(other); this->data = std::move(other.data); return *this; } int main() { ContainerBase<int>* base = new Container<int>(100.4, 200.6); base->print(); return 0; }
相关文章推荐
- 函数指针
- [C/C++]_[初级]_[关于static修饰符的说明]
- [C/C++]_[初级]_[关于static修饰符的说明]
- C++ Operators
- VC++ 简单的打印功能(对话框模式下)
- VC++ 实现文件与应用程序关联
- 操作系统: 银行家算法的分析和代码实现(c++语言)
- C51单片机学习(一):LED和数码管静态显示
- UML之C++类间关系总结
- 在ubuntu中安装eclipse for c++
- Qt C++命名空间
- HDU 1087 Super Jumping! Jumping! Jumping! 菜鸟题解
- 被整数浮点数虐哭过没
- C语言中简单的指针
- C语言总结
- vc++ 中开发汇编语言
- C语言知识汇总
- 如何实现对数ln运算?
- 实战c++中的string系列--string的初始化、删除、转大小写(construct erase upper-lower)
- 实战c++中的string系列--string的初始化、删除、转大小写(construct erase upper-lower)