// constructorofclassderivedfromtemplateclass.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
template <class T>
class A
{
public:
A()
{
}
A(T c)
{
}
A(A &a)
{
}
~A()
{
}
A &operator= (A &a)
{
return *this;
}
};
template <class T>
class B : public A<T>
{
public:
B()// 缺省构造函数将调用模板基类的缺省构造函数
{
}
B(T c) : A(c)// 非缺省构造函数不会调用模板基类的非缺省构造函数
{// 因此如需模板基类的非缺省构造函数,应在派生类中应用初始化参数列表显式调用模板基类的非缺省构造函数
}
//B(B &b) : A(b)// 拷贝构造函数将重载模板基类的拷贝构造函数
//{// 因此如需模板基类的拷贝构造函数,可在派生类中不定义拷贝构造函数,
//}// 或者亦可在派生类中应用初始化参数列表显式调用模板基类的拷贝构造函数
~B()// 析构函数将调用模板基类的析构函数
{
}
//B &operator= (B &b)// 重载赋值运算符将重载模板基类的重载赋值运算符
//{// 因此如需模板基类的重载赋值运算符,应在派生类中不定义重载赋值运算符
// return *this;
//}
};
int _tmain(int argc, _TCHAR* argv[])
{
B<int> b, c;
b = c;
B<int> d(1);
B<int> e(c);
return 0;
}
模板基类派生类的构造函数和析构函数
最新推荐文章于 2024-10-27 15:42:15 发布