读书人

写一个C++泛型集合几个小问题无法解

发布时间: 2012-09-23 10:28:10 作者: rapoo

写一个C++泛型集合,几个问题无法解决,希望专家指点一下,谢谢。
举例 我想自己实现一个C++泛型集合

遇到了2个问题,
1.值类型和引用类型转换
我考虑使用一个 值类型模板,一个引用类型模板
下面是值类型模板代码

2.如何让不同类型的模板对象,用相同的父类指针引用。
例如 :
ValueList<int> *m_intList = new ValueList<int>();
ValueList<double> *m_doubleList = new ValueList<double>();

能否统一类型调用呢?
例如
IList *m_list = new ValueList<int>();
IList *m_list = new ValueList<double>();
类似这种写法 ?


C/C++ code
#include "valuetypenullflag.h"#include "ilist.h"#pragma oncenamespace Generic{    template<class T> class ValueList : public IList    {    private:        T *m_array;        int m_count;        int m_arrayLength;        int m_index;        T m_flag;    public:        ValueList(T flag)        {            this->m_array = new T[4];            this->m_arrayLength = 4;            this->m_index = -1;            this->m_flag = flag;            for (int i = 0 ;i < 4; i++)            {                this->m_array[i] = this->m_flag ;            }        }        ~ValueList(void)        {        }    public:        int Count() const         {             for(int i = 0 ; i < this->m_arrayLength ; i ++)            {                if(this->m_array[i] == this->m_flag)                {                    return i ;                }            }            return this->m_arrayLength;         }        int ArrayLength() const { return m_arrayLength; }    public:        int Add(T item)        {            bool canAdd = false;            for (int i = 0;i< this->m_arrayLength;i++)            {                if(this->m_array[i] == this->m_flag)                {                    canAdd = true;                    this->m_array[i] = item;                    return i;                }            }            if(!canAdd)            {                int i = this->ExtendArray();                this->m_array[i] = item;                return i;            }            return -1;        }        T GetItem(int index)        {            return this->m_array[index];        }    private:        int ExtendArray()        {            int originalArrayLength = this->m_arrayLength;            T *cloneArray = this->m_array;            this->m_array = new T[this->m_arrayLength * 2];            //memset(this->m_array,0,sizeof(int) * this->m_arrayLength * 2);            for(int i = 0 ; i < this->m_arrayLength * 2 ; i++)            {                this->m_array[i] = this->m_flag;            }            this->m_count = this->m_arrayLength;            for (int i = 0; i < this->m_arrayLength; i++)            {                this->m_array[i] = cloneArray[i];            }            this->m_arrayLength *= 2;            return originalArrayLength;        }    };}


谢谢大家帮我解答 :)

[解决办法]
可以,但接口受到很大限制。虚函数要求接口返回值必须一致,像int Count()是可以的, int Add(T item)就不可以。
[解决办法]
C/C++ code
能否统一类型调用呢?  例如IList *m_list = new ValueList<int>();IList *m_list = new ValueList<double>();类似这种写法 ?
[解决办法]
探讨

可以,但接口受到很大限制。虚函数要求接口返回值必须一致,像int Count()是可以的, int Add(T item)就不可以。

[解决办法]
虚函数不就好了...?
[解决办法]
模板强调类型
多态不强调类型
不容易混杂到一起


[解决办法]
_declspec(dllexport) class List

你是要导出dll啊?
[解决办法]
dll恐怕不行

读书人网 >C++

热点推荐