温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++ 中怎么实现数组类泛型编程

发布时间:2021-06-23 17:05:35 来源:亿速云 阅读:514 作者:Leah 栏目:编程语言

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

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

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


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


代码如下:

点击(此处)折叠或打开

模板类实现:

  1. #include<iostream>
    #include <stdlib.h>
     #include <string.h>
    using namespace std;
    template <typename T>
    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 <typename T>
    myarray<T>::~myarray()
    {
            if(this->array != NULL)
            {
                    delete [] this->array;
                    this->array = NULL;
            }
    }
    template <typename T>
    myarray<T>::myarray()
    {
            this->array = NULL;
            this->lenth = 0;
    }
    template <typename T>
    myarray<T>::myarray(unsigned int len)
    {
            this->array = new T[len];
            this->lenth = len;
            memset(this->array,0,sizeof(T)*len);
    }
    template <typename T>
    myarray<T>::myarray(const myarray<T>& a)
    {
            int i;
            this->lenth = a.lenth;
            this->array = new T[a.lenth];
            memset(this->array,0,sizeof(T)*a.lenth);
            for(i=0;i<a.lenth;i++)
            {
                    *(this->array+i) = *(a.array+i);
            }
    }
    template <typename T>
    myarray<T>& myarray<T>::operator=(const myarray<T>& 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;i<a.lenth;i++)
            {
                    *(this->array+i) = *(a.array+i);//元素对象复制调用对象的=操作符重载
            }
            return *this;
    }
    template <typename T>
    T& myarray<T>::operator[](int ind)
    {
            if(ind>=this->lenth)
            {
                    exit(10);
            }
            return *(this->array+ind);
    }
    #include<iostream>
    #include<stdlib.h>
    #include<string.h>
    #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<<a.a<<endl;
                            out<<a.b<<endl;
                            cout<<a.myc;
                            return out;
                    }
                    ~test()
                    {
                            if(myc != NULL)
                            {
                                    free(myc);
                                    myc = NULL;
                            }
                    }
                    test& operator=(const test& a)
                    {
                            if(this->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<test> a(3); //测试class类数组
            a[0] = "asdasd";
            a[1] = "test";
            a[2] = "kkkk";
            myarray<int> b(3); //测试int数组
            b[0] = 1;
            b[1] = 2;
            b[2] = 3;
            myarray<char> c(3); //测试char数组
            c[0] = 'a';
            c[1] = 'b';
            c[2] = 'c';
            myarray<test> d = a;//测试myarray拷贝构造函数
            for(int i=0;i<3;i++)
            {
                    cout<<a[i]<<endl;
                    cout<<d[i]<<endl;
            }
            for(int i=0;i<3;i++)
            {
                    cout<<b[i]<<endl;
            }
            for(int i=0;i<3;i++)
            {
                     cout<<c[i]<<endl;
            }
    }

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI