C++中怎么实现数组类泛型编程-创新互联

创新互联是专业的南开网站建设公司,南开接单;提供成都网站制作、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行南开网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

这篇文章给大家介绍C++ 中怎么实现数组类泛型编程,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

原创:
C++ 简单实现数组类泛型编程示例

1、使用模板来实现泛型编程
2、本数组应该能够存储各种基础类型,各种复杂的类类型
3、应该实现部分操作符重载


其实操作符重载满满的都是套路。


代码如下:

点击(此处)折叠或打开

模板类实现:

  1. #include
    #include 
     #include 
    using namespace std;
    template 
    class myarray
    {
            private:
                    T* array;
                    unsigned int lenth;
            public:
                    myarray();
                    myarray(unsigned int len);
                    myarray(const myarray& a);
                    myarray& operator=(const myarray& b);
                    T& operator[](int ind);
                    ~myarray();
    };
    template 
    myarray::~myarray()
    {
            if(this->array != NULL)
            {
                    delete [] this->array;
                    this->array = NULL;
            }
    }
    template 
    myarray::myarray()
    {
            this->array = NULL;
            this->lenth = 0;
    }
    template 
    myarray::myarray(unsigned int len)
    {
            this->array = new T[len];
            this->lenth = len;
            memset(this->array,0,sizeof(T)*len);
    }
    template 
    myarray::myarray(const myarray& a)
    {
            int i;
            this->lenth = a.lenth;
            this->array = new T[a.lenth];
            memset(this->array,0,sizeof(T)*a.lenth);
            for(i=0;iarray+i) = *(a.array+i);
            }
    }
    template 
    myarray& myarray::operator=(const myarray& a)
    {
            if(this->array != NULL)
            {
                    delete [] this->array;//调用类的析构函数不能用free
                    this->array = NULL;
            }
            this->array = new T[a.lenth];
            this->lenth = a.lenth;
            for(int i=0;iarray+i) = *(a.array+i);//元素对象复制调用对象的=操作符重载
            }
            return *this;
    }
    template 
    T& myarray::operator[](int ind)
    {
            if(ind>=this->lenth)
            {
                    exit(10);
            }
            return *(this->array+ind);
    }
    #include
    #include
    #include
    #include"arra.cpp"
    using namespace std;
    class test
    {
            private:
                    int a;
                    int b;
                    char* myc;
            public:
                    test()
                    {
                            a = 0;
                            b = 0;
                            myc = NULL;
                    }
                    test(const test& a)
                    {
                            this->a = a.a;
                            this->b = a.b;
                            this->myc = (char*)calloc(strlen(a.myc)+1,0);
                            strcpy(this->myc,a.myc);
                    }
                    friend ostream& operator<<(ostream& out,test& a)
                    {
                            out<myc != NULL)
                            {
                                    free(this->myc);
                                    this->myc = NULL;
                            }
                            this->a = a.a;
                            this->b = a.b;
                            this->myc = (char*)calloc(strlen(a.myc)+1,0);
                            strcpy(this->myc,a.myc);
                            return *this;
                    }
                    test& operator=(const char* a)
                    {
                            if(this->myc != NULL)
                            {
                                    free(this->myc);
                                    this->myc = NULL;
                            }
                            if(a == NULL)
                            {
                                    this->myc = (char*)calloc(1,0);
                                    return *this;
                            }
                            this->myc = (char*)calloc(strlen(a)+1,0);
                            strcpy(this->myc,a);
                            return *this;
                    }
    };
    int main()
    {
            myarray a(3); //测试class类数组
            a[0] = "asdasd";
            a[1] = "test";
            a[2] = "kkkk";
            myarray b(3); //测试int数组
            b[0] = 1;
            b[1] = 2;
            b[2] = 3;
            myarray c(3); //测试char数组
            c[0] = 'a';
            c[1] = 'b';
            c[2] = 'c';
            myarray d = a;//测试myarray拷贝构造函数
            for(int i=0;i<3;i++)
            {
                    cout<

关于C++ 中怎么实现数组类泛型编程就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享题目:C++中怎么实现数组类泛型编程-创新互联
转载注明:http://myzitong.com/article/dodoij.html