C++ C++;创建基元数组类

C++ C++;创建基元数组类,c++,class,C++,Class,作为练习,我尝试创建一个类myArray,它充当一个简化的数组类。这是我的标题: #ifndef myArray_h #define myArray_h typedef double ARRAY_ELEMENT_TYPE; class myArray { public: //--constructors myArray(int initMax); // post: Allocate memory during pass by value myArray(const

作为练习,我尝试创建一个类myArray,它充当一个简化的数组类。这是我的标题:

#ifndef myArray_h
#define myArray_h

typedef double ARRAY_ELEMENT_TYPE;

class myArray {

public:
//--constructors
    myArray(int initMax);
    // post: Allocate memory during pass by value

    myArray(const myArray & source);
    // post: Dynamically allocate memory during pass by value

//--destructor
    ~myArray();
    // post: Memory allocated for my_data is deallocated.

//--modifier
    void set(int subscript, ARRAY_ELEMENT_TYPE value);
    // post: x[subscript] = value when subscript is in range.
    //       If not, an error message is displayed.

//--accessor
    ARRAY_ELEMENT_TYPE sub(int subscript) const;
    // post: x[subscript] is returned when subscript is in range.
    //       If not, display an error message and return [0].

private:
ARRAY_ELEMENT_TYPE* my_data;
int my_capacity;
};
#endif
以下是我的实现:

#include "myArray.h"
#include <iostream>
#include <cstring>

using namespace std;

typedef double ARRAY_ELEMENT_TYPE;

//--constructors
myArray::myArray(int initMax)
{
my_capacity = initMax;
}

myArray::myArray(const myArray & source)
{
int i;
my_data = new ARRAY_ELEMENT_TYPE[source.my_capacity];

for(i=0; i < my_capacity; i++)
    my_data[i] = source.sub(i);
}

//--destructor
myArray::~myArray()
{
delete [ ] my_data;
}

//--modifier
void myArray::set(int subscript, ARRAY_ELEMENT_TYPE value)
{
if(subscript > my_capacity - 1)
{
    cout << "**Error: subscript " << subscript << " not in range 0.." << my_capacity-1 << ". The array is unchanged." << endl;
}
else
    my_data[subscript] = value;
}

//--accessor
ARRAY_ELEMENT_TYPE myArray::sub(int subscript) const
{
if(subscript >= my_capacity)
{
    cout << "**Error: subscript " << subscript << " not in range 0.." << my_capacity-1 << ". Returning first element." << endl;
    cout << my_data[0];
}
else
{
    return my_data[subscript];
}
}
#包括“myArray.h”
#包括
#包括
使用名称空间std;
typedef双数组元素类型;
//--建设者
myArray::myArray(int initMax)
{
我的容量=初始最大值;
}
myArray::myArray(常量myArray和源)
{
int i;
my_data=新数组元素类型[源.my_容量];
对于(i=0;i我的容量-1)
{

coutmyArray
构造函数不为
my_data
分配内存。第一次调用
set
时,它试图写入未初始化的指针。这会导致未定义的行为,但很可能会崩溃

您应该将构造函数更改为

myArray::myArray(int initMax)
{
    my_capacity = initMax;
    my_data = new ARRAY_ELEMENT_TYPE[my_capacity];
}
您还可以考虑代码中的一些其他问题

在“设置”中,测试

if(subscript > my_capacity - 1)
应该是

if(subscript < 0 || subscript > my_capacity - 1)
if(下标<0 | |下标>my|u容量-1)
或者您可以将
subscript
参数更改为具有类型
unsigned int


sub
中,行
cout除了在当前实现中丢失分配之外,您还动态分配内存。不需要在堆上分配简单的数组类型。
std::array
集合正是您想要做的。我敦促您查看它的实现举个例子(如果这只是一个学术练习)。如果这是一个生产代码库,使用已经编写和测试的代码


别忘了。三分之二还不错,但您缺少了一个拷贝分配操作符。这解决了大部分问题。现在我收到一条警告,不是所有的返回路径在
中都返回一个值,但我无法确定如果(下标>=我的\u容量)会超出
的范围
else
。上次调用
a.set(-1,-1.1)
时,我遇到了一个崩溃,输出为
1.1-1.\IND
,还有一个堆损坏调试错误。我更新了我的答案,并记录了导致这些问题的原因。我错过了您的最后一行,从而解决了#IND问题。谢谢您的帮助(因为我还不能投票)。
std::array
是静态大小的。在我们明年获得动态大小的数组和
std::dynarray
之前,如果在编译时不知道大小,我们需要动态分配。(问题确实说明这是一个练习,所以“使用
vector
”也不是一个有用的答案)。我没有说明要使用
std::array
std::vector
(除非是用于生产代码库)。我非常明确地说,他应该查看
std::array
的实现,看看它是如何实现的。他当前的实现创建了一个不可调整大小的动态分配数组(换句话说,他知道它在编译时有多大,但无论如何都是在堆上分配的)。不,当前的实现将大小作为运行时构造函数参数,而不是像
array
那样作为编译时模板参数。在明年之前,您需要动态分配来处理这个问题。(在本例中,大小是一个常量;但没有迹象表明该类应限制为编译时值)。
if(subscript < 0 || subscript > my_capacity - 1)
myArray::myArray(int initMax)
{
my_capacity = initMax;
my_data = new ARRAY_ELEMENT_TYPE[my_capacity]; //You missed this
}