gpt4 book ai didi

c++ - 由算术运算符成员函数修改的动态数组的复制构造函数

转载 作者:行者123 更新时间:2023-11-30 02:06:58 25 4
gpt4 key购买 nike

虽然之前已经讨论过copy constructor这方面的问题,但是我没能找到以下问题的最佳解决方案:如何通过返回包含算术结果的类对象来调用copy constructor (*,/,+)由成员函数执行 - 如下所示?

#include <iostream>
#include "DataContainer.hh"
using namespace std;

int main() {
DATA_TYPE numbers1[] = {1,2,3,4,5,6,7,8,9,10};
DATA_TYPE numbers2[] = {10,9,8,7,6,5,4,3,2,1};
DataContainer *dataContainer1 = new DataContainer(numbers1,10);
DataContainer *dataContainer2 = new DataContainer(numbers2,10);
dataContainer1->printArray();
dataContainer2->printArray();
cout << ">> Multiplication:" << endl;
(*dataContainer1 * (*dataContainer2)).printArray();
cout << ">> Addition:" << endl;
(*dataContainer1 + (*dataContainer2)).printArray();
cout << ">> Division:" << endl;
(*dataContainer1 / (*dataContainer2)).printArray();

delete dataContainer1;
delete dataContainer2;
return 0;
}


#ifndef DATA_CONTAINER_HH
#define DATA_CONTAINER_HH
#define DATA_TYPE float

class DataContainer {
public:
DataContainer(void);
DataContainer(DATA_TYPE *array, int size);
DataContainer(const DataContainer& dataContainer);
DataContainer operator*(DataContainer);
DataContainer operator/(DataContainer);
DataContainer operator+(DataContainer);
void printArray();

private:
DATA_TYPE *m_array;
int m_size;
};
#endif


#include "DataContainer.hh"
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

DataContainer::DataContainer()
{
}
DataContainer::DataContainer(DATA_TYPE *array, int size)
{
m_size = size;
m_array = new DATA_TYPE[m_size];
for(int i=0; i<m_size; i++)
m_array[i] = array[i];
}

DataContainer::DataContainer(const DataContainer& dataContainer)
: m_size(dataContainer.m_size), m_array(new DATA_TYPE[dataContainer.m_size])
{
std::copy(dataContainer.m_array, dataContainer.m_array + dataContainer.m_size, m_array);
}

void DataContainer::printArray()
{
for(int i=0; i<m_size; i++)
cout << m_array[i] << " ";
cout << endl;
}

DataContainer DataContainer::operator*(DataContainer container) {
DATA_TYPE *temp;
for (int i=0;i<m_size;i++) temp[i] = m_array[i] * container.m_array[i];
return DataContainer(temp, m_size);
}
DataContainer DataContainer::operator/(DataContainer container) {
DATA_TYPE *temp;
for (int i=0;i<m_size;i++) temp[i] = m_array[i] / container.m_array[i];
return DataContainer(temp, m_size);
}
DataContainer DataContainer::operator+(DataContainer container) {
DATA_TYPE *temp;
for (int i=0;i<m_size;i++) temp[i] = m_array[i] + container.m_array[i];
return DataContainer(temp, m_size);
}


输出结果如下:

1 2 3 4 5 6 7 8 9 10 
10 9 8 7 6 5 4 3 2 1
>> Multiplication:
Segmentation fault


似乎动态内存分配不起作用。任何帮助是极大的赞赏。非常感谢。-曼纽尔

最佳答案

您的 operator* 定义了一个指针,但没有将其初始化为指向分配的内存。然后您尝试写入它,因此出现段错误。

我不太确定为什么您的类持有指向原始内存的指针,而不是仅仅使用 std::vector 或其他东西。这样您就无需担心动态内存分配问题。

关于c++ - 由算术运算符成员函数修改的动态数组的复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com