gpt4 book ai didi

c++ - 动态对象数组不工作

转载 作者:行者123 更新时间:2023-11-28 04:52:46 29 4
gpt4 key购买 nike

我有一个 parent 和两个 child ,我想创建一个包含 child 的动态数组(不允许使用 vector )。我创建了类和所有内容,没有收到任何编译错误或警告,但是当我尝试访问数组成员并打印其内容时,没有任何反应。

我知道这里可能有一些糟糕的编码风格,但我只想知道为什么数组不起作用。我认为这是因为我创建了一个对象数组,而不是一个指向对象的指针数组,我不确定。

(我从代码中删除了第二个 child ,因为它在这里无关紧要)

#pragma warning(disable:4996)
using namespace std;
#include <iostream>
#include <cstdlib>
#include <cctype>
#include "date.h"

#define ADDRESS_SIZE 512
#define NAME_SIZE 128
#define DATE_SIZE 128



class Treatment {
protected:
int treatment_ID;
int patient_ID;
Date treatment_date;
int treatment_costs;
public:
Treatment() {}
Treatment(int treatment_ID, int patient_ID, int treatment_costs, char treatment_date[DATE_SIZE]) {
this->treatment_ID = treatment_ID;
this->patient_ID = patient_ID;
this->treatment_costs = treatment_costs;
this->treatment_date.convert_date(treatment_date);
}

void set_treatment_ID(int treatment_ID) {
this->treatment_ID = treatment_ID;
}

void read_treatment_date(void) {
treatment_date.read_date();
}

void set_treatment_date(char _date[DATE_SIZE]) {
treatment_date.convert_date(_date);
}

void set_patient_ID(int patient_ID) {
this->patient_ID = patient_ID;
}
void set_treatment_costs(int treatment_costs) {
this->treatment_costs = treatment_costs;
}

int get_treatment_ID(void) {
return treatment_ID;
}
int get_patient_ID(void) {
return patient_ID;
}
int get_treatment_costs(void) {
return treatment_costs;
}

};

class Outside_treatment : public Treatment {
private:
int clinic_num;
//class doctor;
public:
Outside_treatment() {}
Outside_treatment(int clinic_num, int treatment_ID, int patient_ID, int treatment_costs, char treatment_date[DATE_SIZE]) :Treatment(treatment_ID, patient_ID, treatment_costs, treatment_date) {
this->clinic_num = clinic_num;
}
void set_clinic_num(int clinic_num) {
this->clinic_num = clinic_num;
}
int get_clinic_num() {
return this->clinic_num;
}

void print_outside_treatment(void) {
cout << "The clinic num is " << clinic_num << "\n";
cout << "The treatment ID is " << treatment_ID << "\n";
cout << "The patient_ID is " << patient_ID << "\n";
cout << "The treatment costs are " << treatment_costs << "\n";
treatment_date.print_date();
cout << " treatment date in compare format is " << treatment_date.get_compare();

}

};

class Dynarray {
private:
Treatment *pa;
int length;
int nextIndex;
public:
Dynarray() {
pa = new Treatment[10];
length = 10;
nextIndex = 0;
}
~Dynarray() {
delete[]pa;
}
void add(Treatment &add) {
Treatment *pnewa;
if (nextIndex == length) {
length += 10;
pnewa = new Treatment[length];
for (int i = 0; i < nextIndex; ++i) {
pnewa[i] = pa[i];
}
delete[]pa;
pa = pnewa;
}
pa[nextIndex++] = add;
}
Treatment &operator[](int index) {
return *(pa + index);
}
};

int main(void) {
Outside_treatment it;

cout << "Enter the patient ID\n";
int p_id;
cin >> p_id;
it.set_patient_ID(p_id);
cin.ignore();
cout << "set treatment date\n";
it.read_treatment_date();

cout << "enter costs\n";
int costs;
cin >> costs;
it.set_treatment_costs(costs);

cout << "enter section num\n";
int sc_num;
cin >> sc_num;
it.set_clinic_num(sc_num);

it.print_outside_treatment();

Dynarray da;

da.add(it);

Treatment *yetanotherpointer = &da[0];


int i = yetanotherpointer->get_patient_ID();
cout << i << endl;

while (1);
};

最佳答案

您正在创建一组 Treatment 对象实例。因此,您根本无法存储任何派生类型。如果您尝试将派生类型的对象分配给 Treatment 对象,it will get sliced .

您的预感是正确的。由于您处理的是多态类型,因此需要在数组中存储指向对象的指针,而不是实际 对象。然后你可以存储指向任何派生类型对象的Treatment*指针,例如:

class Treatment {
...
public:
...

// make the base class destructor virtual so calling
// 'delete' on a base class pointer will invoke derived
// destructors. Otherwise, if a derived class has any
// data members with a non-trivial destructor, you will
// cause leaking. When writing polymorphic classes, it
// is common practice to always define a virtual destructor,
// just in case...
//
virtual ~Treatment() {} // <-- add this
..
};

class Dynarray {
private:
Treatment **pa;
int length;
int capacity;

// prevent copying the array, otherwise you risk
// memory errors if multiple arrays own the same
// objects being pointed at and try to free them
// multiple times. Copying pointers to owned objects
// is not safe, you need a cloning mechanism to
// make deep-copies of derived types so a copied
// array can point to its own objects...
Dynarray(const Dynarray &) {}
Dynarray& operator=(const Dynarray &) {}

public:
Dynarray(int initialCapacity = 10) {
pa = new Treatment*[initialCapacity];
capacity = initialCapacity;
length = 0;
}

~Dynarray() {
for (int i = 0; i < length; ++i)
delete pa[i];
delete[] pa;
}

void add(Treatment *add) {
if (length == capacity) {
Dynarray temp(capacity + 10);
for (int i = 0; i < length; ++i) {
temp.pa[i] = pa[i];
}
Treatment *ptr = temp.pa;
temp.pa = pa;
pa = ptr;
capacity = temp.capacity;
}

pa[length++] = add;
}

Treatment* operator[](int index) {
return pa[index];
}
};

int main(void) {
Outside_treatment *ot = new Outside_treatment;

cout << "Enter the patient ID\n";
int p_id;
cin >> p_id;
ot->set_patient_ID(p_id);
cin.ignore();
cout << "set treatment date\n";
ot->read_treatment_date();

cout << "enter costs\n";
int costs;
cin >> costs;
ot->set_treatment_costs(costs);

cout << "enter section num\n";
int sc_num;
cin >> sc_num;
ot->set_clinic_num(sc_num);

ot->print_outside_treatment();

Dynarray da;
da.add(ot);

Treatment *t = da[0];

int i = t->get_patient_ID();
cout << i << endl;

while (1);
};

另一方面,如果数组不需要拥有被指向的对象,只是存储指针,你可以在析构函数中移除delete循环,而调用者不是需要使用 new 创建正在存储的对象,并且制作数组的拷贝是安全的,例如:

class Dynarray {
private:
Treatment **pa;
int length;
int capacity;

public:
Dynarray(int initialCapacity = 10) {
pa = new Treatment*[initialCapacity];
capacity = initialCapacity;
length = 0;
}

// coping is OK since the objects being pointed
// at are not owned by the array!
Dynarray(const Dynarray &src) {
pa = new Treatment*[src.capacity];
capacity = src.capacity;
length = src.length;
for (int i = 0; i < length; ++i) {
pa[i] = src.pa[i];
}
}

Dynarray& operator=(const Dynarray &rhs) {
if (&rhs != this) {
Dynarray temp(rhs);
Treatment **ptr = temp.pa;
temp.pa = pa;
pa = ptr;
capacity = rhs.capacity;
length = rhs.length;
}
return *this;
}

~Dynarray() {
delete[] pa;
}

void add(Treatment *add) {
if (length == capacity) {
Dynarray temp(capacity + 10);
for (int i = 0; i < length; ++i) {
temp.pa[i] = pa[i];
}
Treatment **ptr = temp.pa;
temp.pa = pa;
pa = ptr;
capacity = temp.capacity;
}

pa[length++] = add;
}

Treatment* operator[](int index) {
return pa[index];
}
};

int main(void) {
Outside_treatment ot;

cout << "Enter the patient ID\n";
int p_id;
cin >> p_id;
ot.set_patient_ID(p_id);
cin.ignore();
cout << "set treatment date\n";
ot.read_treatment_date();

cout << "enter costs\n";
int costs;
cin >> costs;
ot.set_treatment_costs(costs);

cout << "enter section num\n";
int sc_num;
cin >> sc_num;
ot.set_clinic_num(sc_num);

ot.print_outside_treatment();

Dynarray da;
da.add(&ot);

Treatment *t = da[0];

int i = t->get_patient_ID();
cout << i << endl;

while (1);
};

关于c++ - 动态对象数组不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47803750/

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