博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
模板特化疑问
阅读量:7231 次
发布时间:2019-06-29

本文共 2758 字,大约阅读时间需要 9 分钟。

hot3.png

    最近在学习模板,对模板特化做了一些实验

    下面是代码

#include 
using namespace std;#include
template
class zVector{public: /** * @brief zVector * @param size */ zVector(int size = 10):m_size(size),p(new T[size]) {} ~zVector(){ delete p; } /** * @brief operator [] * @param index * @return */ T& operator[](int index) const { return p[index]; }private: int m_size; T *p;};template <>class zVector
{public: zVector(int size = 10):m_size(size),p(new void*[size]) {} ~zVector(){ delete p; } void*& operator[](int index) const { return p[index]; }private: int m_size; void* *p;};//typedef zVector
zvVector;template
class zVector
: public zVector
{public: T*& operator [](int index) const { return (T*&)zVector
::operator [](index); }};//template
//class zVector
: public zvVector//{//public:// T*& operator [](int index) const {// return (T*&)zvVector::operator [](index);// }//};int main(){ zVector
intv; zVector
doublev; int i = 1; double j = 2.11; intv[0] = &i; cout << *intv[0] << endl; doublev[0] = &j; cout << *doublev[0]; return 0;}

能够运行且符合我的期望,不过这个是我写的。书上的例子是这个
#include 
using namespace std;#include
template
class zVector{public: /** * @brief zVector * @param size */ zVector(int size = 10):m_size(size),p(new T[size]) {} ~zVector(){ delete p; } /** * @brief operator [] * @param index * @return */ T& operator[](int index) const { return p[index]; }private: int m_size; T *p;};//template <>//class zVector
//{//public:// zVector(int size = 10):m_size(size),p(new void*[size]) {}// ~zVector(){// delete p;// }// void*& operator[](int index) const {// return p[index];// }//private:// int m_size;// void* *p;//};typedef zVector
zvVector;//template
//class zVector
: public zVector
//{//public:// T*& operator [](int index) const {// return (T*&)zVector
::operator [](index);// }//};template
class zVector
: public zvVector{public: T*& operator [](int index) const { return (T*&)zvVector::operator [](index); }};int main(){ zVector
intv; zVector
doublev; int i = 1; double j = 2.11; intv[0] = &i; cout << *intv[0] << endl; doublev[0] = &j; cout << *doublev[0]; return 0;}

可是就报错了,

我分析发现好像是
typedef zVector<void*> zvVector;
模板特化没有起到作用
这是其中的一个错误
cannot call member function 'T*& zVector<T*>::operator[](int) const [with T = void]' without object
表示void*没有特化,即没有特化T=void*时的模板,所以报错了。
在第一个中我指定特化了,所以能够运行了。
我使用的编译器是g++4.6.3

转载于:https://my.oschina.net/u/854744/blog/418383

你可能感兴趣的文章
问题:关于一个坛友的html布局实现
查看>>
nginx 图片防盗链
查看>>
【SICP练习】109 练习3.22
查看>>
swift - UIWebView 和 WKWebView(iOS12 之后替换UIWebView)
查看>>
tkinter内嵌Matplotlib系列(一)之解读官网教材
查看>>
光伏工商业屋顶 Lora 组网监控方案
查看>>
学号 2017-2018-20172309 《程序设计与数据结构》第10周学习总结
查看>>
命令收集
查看>>
mysql 1449 : The user specified as a definer (\'root\'@\'%\') does not exist 解决方法
查看>>
mysql 定时任务的使用
查看>>
Css实现拖动效果
查看>>
KVO 键值观察者
查看>>
C# 有符号整数 无符号整数
查看>>
第15讲 | 深入区块链技术(七):哈希与加密算法
查看>>
第33讲 | 区块链与供应链(二)
查看>>
scss rem 转换函数
查看>>
Shell脚本中的 测试开关 和 特殊参数
查看>>
Ubuntu下安装Vmware workstation
查看>>
Activity的四种启动模式 分类: Android ...
查看>>
TPS、并发用户数、吞吐量关系
查看>>