模板类中的成员函数定义返回值为类中的typedef类型时候注意
2015-11-09 22:44
513 查看
如果模板类中的成员要访问类中的typedef类型必须加上关键字typename来指明它是一个类型。
如一下代码中的那个成员函数size。
[cpp] view
plaincopy
#include <iostream>
#include <string>
template<typename Type>
class List
{
public:
typedef unsigned size_type;
public:
List();
void push_back(const Type &value);
void push_front(const Type &value);
Type& front() const;
Type& back() const;
size_type size() const;
~List();
private:
struct DataType
{
Type Value;
struct DataType *next;
}head;
struct DataType *end;
};
template<typename Type>
List<Type>::List()
{
head.next = 0;
end = &head;
}
template<typename Type>
void List<Type>::push_back(const Type &value)
{
DataType *tmp = new DataType;
tmp->Value = value;
tmp->next = 0;
end->next = tmp;
end = tmp;
}
template<typename Type>
void List<Type>::push_front(const Type &value)
{
DataType *tmp = new DataType;
tmp->Value = value;
tmp->next = 0;
tmp->next = head.next;
head.next = tmp;
}
template<typename Type>
Type& List<Type>::front() const
{
return head.next->Value;
}
template<typename Type>
Type& List<Type>::back() const
{
return end->Value;
}
template<typename Type>
typename List<Type>::size_type List<Type>::size() const
{
size_type iSize = 0;
struct DataType *begin = &head;
while (begin->next)
{
++iSize;
}
return iSize;
}
template<typename Type>
List<Type>::~List()
{
struct DataType *tmp = &head;
while (tmp = tmp->next)
{
head.next = tmp->next;
free(tmp);
tmp = &head;
}
}
int main()
{
List<int> list;
list.push_back(100);
list.push_front(200);
std::cout << list.front() << "/t" << list.back() << std::endl;
system("pause");
return 0;
}
如一下代码中的那个成员函数size。
[cpp] view
plaincopy
#include <iostream>
#include <string>
template<typename Type>
class List
{
public:
typedef unsigned size_type;
public:
List();
void push_back(const Type &value);
void push_front(const Type &value);
Type& front() const;
Type& back() const;
size_type size() const;
~List();
private:
struct DataType
{
Type Value;
struct DataType *next;
}head;
struct DataType *end;
};
template<typename Type>
List<Type>::List()
{
head.next = 0;
end = &head;
}
template<typename Type>
void List<Type>::push_back(const Type &value)
{
DataType *tmp = new DataType;
tmp->Value = value;
tmp->next = 0;
end->next = tmp;
end = tmp;
}
template<typename Type>
void List<Type>::push_front(const Type &value)
{
DataType *tmp = new DataType;
tmp->Value = value;
tmp->next = 0;
tmp->next = head.next;
head.next = tmp;
}
template<typename Type>
Type& List<Type>::front() const
{
return head.next->Value;
}
template<typename Type>
Type& List<Type>::back() const
{
return end->Value;
}
template<typename Type>
typename List<Type>::size_type List<Type>::size() const
{
size_type iSize = 0;
struct DataType *begin = &head;
while (begin->next)
{
++iSize;
}
return iSize;
}
template<typename Type>
List<Type>::~List()
{
struct DataType *tmp = &head;
while (tmp = tmp->next)
{
head.next = tmp->next;
free(tmp);
tmp = &head;
}
}
int main()
{
List<int> list;
list.push_back(100);
list.push_front(200);
std::cout << list.front() << "/t" << list.back() << std::endl;
system("pause");
return 0;
}
相关文章推荐
- .Net控件创建热键
- 程序员进阶(转)
- .Net控件创建热键
- 在ubuntu下配置windows访问ubuntu环境
- 分治策略
- OC-NSDate
- libvirt-virsh命令
- 【NOIP2015】总结与反思
- 1个Nginx+2个tomcat+2个redis服务器(主从复制)实现反向代理,实现动静分离。
- java队列queue
- 运行127.0.0.1
- BING(Binarized Normed Gradients for Objectness )的一些分享
- DHCP Server软件使用教程
- SSLServerSocket、SSLSocket简单例子
- #define#ifndef#endf 防止头文件重复包含,心得,供大家参考
- 享元模式
- 【随机过程】马尔可夫链(2)
- IOS AutoLayout详解(三)用代码实现(附Demo下载)
- iOS中的数据持久化方式
- Linux网络相关设置