Код: Выделить всё
class Foo{
Bar** bars = new Bar*10;
};
class Bar{
string temp;
};
Код: Выделить всё
Foo* foos = new Foo[5];
Код: Выделить всё
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 10; j++)
{
delete foos[i].bars[j];
}
delete[] foos[].bars;
}
delete[] foos;
Я получаю исключение каждый раз, когда пытаюсь удалить
Также пытался просто удалить массив внутри каждого объекта, без удаление объектов массива также не удалось.
Полный код:
Код: Выделить всё
#include
#include
#include
#include
#include
int MAX_BOOKS = 10;
using namespace std;
class Book {
private:
string title, author;
mutable bool canBook;
public:
Book(string x = "", string y = "", bool z = false) : title(x), author(y), canBook(z) {}
~Book() {}
Book(const Book& t)
{
this->title = t.title;
this->author = t.author;
this->canBook = t.canBook;
}
void print() {
string available;
cout name = name;
}
void setsurname(string surname)
{
this->surname = surname;
}
};
Person Person;
float penalty;
Book** booked = new Book*[MAX_BOOKS]; //books
int bookedNum; //currently booked
public:
//constructor
Reader(string a = "name", string b = "surname", float x = 0)
: Person(a, b), penalty(x), bookedNum(0) {
for (int i = 0; i < MAX_BOOKS; ++i)
{
booked[i] = new Book();
}
//booked = new Book * [MAX_BOOKS];
}
// copy-constructor
Reader(const Reader& t)
: Person(t.Person), penalty(t.penalty), bookedNum(t.bookedNum) {
booked = new Book * [bookedNum];
for (int i = 0; i < bookedNum; ++i) {
booked[i] = new Book(*t.booked[i]);
}
}
//destructor
~Reader() {
for (int i = 0; i < MAX_BOOKS; ++i) {
delete booked[i];
}
delete[] booked;
}
//copy-swap
Reader& operator=(const Reader& other){
Reader temp(other);
std::swap(temp.Person, Person);
std::swap(temp.penalty, penalty);
std::swap(temp.booked, booked);
std::swap(temp.bookedNum, bookedNum);
return *this;
}
Reader& operator=(Reader&& other) noexcept
{
if (this != &other)
{
this->Person = move(other.Person);
this->penalty = other.penalty;
this->bookedNum = other.bookedNum;
delete[] this->booked;
this->booked = other.booked;
other.booked = nullptr;
other.penalty = 0.0f;
other.bookedNum = 0;
}
return *this;
}
void setname(string name) {
this->Person.setname(name);
}
void setsurname(string surname) {
this->Person.setsurname(surname);
}
void setpenalty(float penalty) {
this->penalty = penalty;
}
void setliczbaKsiazek(int liczba) {
this->bookedNum = liczba;
}
string getname() {
return this->Person.getname();
}
string getsurname() {
return this->Person.getsurname();
}
float getpenalty() {
return penalty;
}
int getliczbaKsiazek() {
return bookedNum;
}
Book** getwypozyczona() {
return booked;
}
void print()
{
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78432401/proper-memory-freeing[/url]
Мобильная версия