您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: