c++ - 复制构造函数不从类对象中获取数组大小

标签 c++ arrays templates operator-overloading copy-constructor

我有一个接受数组及其大小的类。在类中,我有一个索引运算符“[]”,它已被重载以将其内容传递给类成员函数并返回该函数。它还会检查我是否正在访问超出数组大小的内容。

     this_type operator[] (int index)  
    {
        assert (index > 0 && index<=len);
        at(index);
        return c_arr()[index];
    }

我为它做了一个拷贝构造函数

   //constructor
    my_array(this_type const arr[], int size)
    {
    len = size;
    assert(arr != NULL);
    assert(size >= 0);

    this->arr = new this_type[size];
    for (int i = 0; i < size; i++)
        (*this).arr[i] = arr[i];
    }
    //copy constructor
    my_array( const my_array<this_type>  & arr)
    {
    this->arr = new this_type[sizeof(arr)];
    memcpy(this->arr, arr.arr, sizeof(this_type)*arr.len);
    }
    my_array(int size)
    {
    len = size;
    assert(size >= 0);

    this->arr = new this_type[size];
    }

但是在调用成员函数“len”时好像没有传入数组大小值。有什么想法吗?

    #include <iostream>
    #include <cassert>
    #include <assert.h>
    using namespace std;

    template <class this_type> 
    class my_array
    {
    private:
    this_type *arr;
    int len;

    int sum()
    {
        int sum;
        for (int i = 0; i < len; i++)
            sum += arr[i];
    }

    public:


    int size() const
    {
    return len;
    }

    this_type &at(int index)
    {
    assert( index >= 0 && index < len);
    return arr[index];
    }

    my_array(this_type const arr[], int size)
    {
    len = size;
    assert(arr != NULL);
    assert(size >= 0);

    this->arr = new this_type[size];
    for (int i = 0; i < size; i++)
        (*this).arr[i] = arr[i];
    }
    my_array( const my_array<this_type>  & arr)
    {
    this->arr = new this_type[sizeof(arr)];
    memcpy(this->arr, arr.arr, sizeof(this_type)*arr.len);
    }
    my_array(int size)
    {
    len = size;
    assert(size >= 0);

    this->arr = new this_type[size];
    }
    ~my_array()
    {
    delete[] arr;
    }

    this_type* c_arr()
    {
        return arr;
    }

     this_type & operator[] (int index)
        {
        assert (index > 0 && index<=len);
         at(index);
         return c_arr()[index];
    }

     this_type operator[] (int index)  
    {
        assert (index > 0 && index<=len);
        at(index);
        return c_arr()[index];
    }
    friend istream & operator>>(istream &lhs, my_array<this_type> &rhs);

    friend ostream & operator<<(ostream &lhs, my_array<this_type> &rhs);
    };
    template <class this_type> 
    ostream & operator<<(ostream &lhs, my_array<this_type>&rhs)
    {
    for ( int i = 0; i < rhs.size(); i++)
        lhs << rhs.arr[i] << endl;
    return lhs;
    }
    template <class this_type> 
    istream & operator>>(istream &lhs, my_array<this_type> &rhs)
    {
    for ( int i = 0; i < rhs.size(); i++)
        lhs >> rhs.arr[i];
    return lhs;
    }




    int main()
    {
    char arra[5]={'c','o','d','e','s'};

    my_array<char> arr(arra,5), arr1(5), arr2(arr);
       for(int t=0;t< 9;t++)
      {
       //use the operator that is attached to the class instance
        cout << arr2[t];

   }
    for(int t=0;t< 9;t++)
    {
    cout<<arr2.c_arr()[t];
    }
    return 0;
    }

最佳答案

this->arr = new this_type[sizeof(arr)];

您的复制构造函数中的这一行不正确。 arr 是您的 my_array 类的一个对象。 sizeof(arr) 是一个编译时常量,完全独立于为数组分配的元素数量。这些元素甚至不包含在类中。它们在免费商店中,类持有指向它们的指针。你想要的是:

this->arr = new this_type[arr.len];

您还想分配给正在构造的对象的 len 成员。

this->len = arr.len;

我不得不说,除非您创建此类是为了学习目的,否则只需使用 std::vector .

关于c++ - 复制构造函数不从类对象中获取数组大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19850795/

相关文章:

javascript - MustacheJS 不会渲染调用其他函数的函数的值

C++函数接受函数指针和非静态成员函数作为参数

c++ - 访问模板参数的 protected 成员

c++ - 关于文件夹中的文件名

c++ - 在不同的计算机上运行 Qt 应用程序

使用 gcc 的 c++ makefile - 从子文件夹列表生成源文件列表

javascript - 合并2个数组,如果不重复则填充0

javascript - 如何在 HTML 中查看数组

c++ - c++中没有虚函数的多重继承

javascript - 如何解决未捕获的类型错误 : Cannot read property 'length' of undefined of my Global varible?