复制构造函数不从类对象中获取数组大小
Copy constructor not taking in the array size from a class object
我有一个类,它接受数组及其大小。 在类中,我有一个索引运算符"[]",它已被重载以将其内容传递给类成员函数并返回该函数。它还检查我是否正在访问数组大小之外的内容。
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
.
相关文章:
- 如何在 nlohmann 的 json 库中获取数组长度?
- 从用户那里获取数组大小的值
- 如何在VS2019中获取数组的任意对齐?
- 我如何获取数组的大小,以便我可以从函数声明所述数组
- 根据字符获取数组的索引
- 尝试递归获取数组元素的总和
- 用于获取数组长度的指针数学
- Socket.io客户端C++,从Socket.on获取数组
- C++ nlohmann JSON 获取数组名称
- 练习循环和使用循环 1 次以及如何获取数组的最大值
- 如何在函数参数中通过引用获取数组
- 如何获取数组中字符串元素的字符数?[C++]
- 如何声明一个未知大小的数组,然后输入直到我想要,然后获取数组的大小
- 无法获取数组内数组的总和
- 尝试获取数组中的最小元素时崩溃
- 获取数组类型的无效错误
- 如何从用户获取数组输入
- 使用位集获取数组项的条件总和,而无需使用循环
- 如何以模式获取数组中的数据?
- 如何获取数组中每个数字的平方根 (C++)