在C ++中为类分配内存 [英] Allocating memory for class in C++
问题描述
这是我的类:
using namespace std;
Class Book {
public:
Book();
Book(vector<string>*, string, int);
Book(const Book&);
~Book();
Book& operator=(const Book&);
void update(vector<string>*);
void update(string);
void update(int);
int getYear() const{
return year;
};
string getTitle() const{
return title;
};
bool operator==(const Book&);
bool operator!=(const Book&);
friend std::ostream& operator<<(std::ostream&, const Book&);
void getAuthors();
private:
vector<string>* authors;
string title;
int year;
};
#endif /* BOOK_H */
:
#include "Book.h"
using namespace std;
Book::Book():year(0), title(NULL), authors(NULL){}
Book::Book(vector<string>* bookauthors,string booktitle, int bookyear ){
authors = bookauthors;
title = booktitle;
year = bookyear;
}
Book::Book(const Book& aBook){
authors = aBook.authors;
title = aBook.title;
year = aBook.year;
}
Book::~Book(){
delete authors;
delete &title;
delete &year;
}
bool Book::operator==(const Book &aBook){
if(getYear() == aBook.getYear() && getTitle() == aBook.getTitle())
return true;
else return false;
}
bool Book::operator != (const Book &aBook){
if(getYear() != aBook.getYear() && getTitle() != aBook.getTitle())
return true;
else return false;
}
Book& Book::operator =(const Book& rhs){
if(this != &rhs){
authors = rhs.authors;
title = rhs.title;
year = rhs.year;
}
return *this;
}
void Book::update(int newyear){
year = newyear;
}
void Book::update(string newtitle){
title = newtitle;
}
void Book::update(vector<string>* newauthors){
authors = newauthors;
}
std::ostream& operator <<(std::ostream& os, const Book& b){
os<<b.getTitle()<<", "<<b.getYear();
return os;
}
这里是它运行的主文件:
Here is the main file where it runs:
#include "Book.h"
#include <iostream>
#include <limits.h>
//This is the test funcion posted on the class website
using namespace std;
int main(){
//testing constructor
vector<string> authors;
authors.push_back("Ritchie");
authors.push_back("Kernighan");
Book a(&authors, "C", 1990);
authors.push_back("Whatever");
cout << "Book a is: " << a << endl;
cout << "Expected: (C, 1990, Ritchie & Kernighan)" << endl;
//testing copy constructor
Book b(a);
a.update(&authors);
cout << "Book b is: " << b << endl;
cout << "Expected: (C, 1990, Ritchie & Kernighan)" << endl;
//testing constructor
vector<string> authors2;
authors2.push_back("Crockford");
Book c(&authors2, "JavaScript", 2008);
cout << "Book c is: " << c << endl;
cout << "Expected: (JavaScipt, 2008, Crockford)" << endl;
//testing assignment operator
authors2.push_back("whatever");
a=c;
cout << "Book a is changed to: " << a << endl;
cout << "Expected: (JavaScipt, 2008, Crockford)" << endl;
for(int i=0; i < 200000000; i++)
b=c;
cout << "Book b is changed to: " << b << endl;
cout << "Expected: (JavaScipt, 2008, Crockford)" << endl;
}
当我运行它时,我会得到这个:
I keep getting this when I run it:
bookclass(58316)malloc:对象0x7fff522d78b0的错误:未分配的指针未分配* 在malloc_error_break中设置断点以调试
bookclass(58316) malloc: * error for object 0x7fff522d78b0: pointer being freed was not allocated* set a breakpoint in malloc_error_break to debug
我是C ++的新手,所以我不知道如何分配内存。我尝试使用 malloc
,但它没有工作。
I'm new to C++ so I'm not sure how to allocate the memory. I tried using malloc
and it didn't work.
推荐答案
成员位于对象内部,即,它们的存储器被分配有 Book
对象,并且既不能也不需要 delete
显式地。基本上,你需要使用 new
与对 delete
的调用匹配你的显式分配,但是你永远不需要释放
The members are located inside the object, i.e., the memory for them is allocated with the Book
object and neither can nor need to delete
the memory explicitly. Basically, you need to match your explicit allocations using new
with calls to delete
but you never need to release something which isn't allocated somewhere explicitly.
也就是说,当您尝试删除标题
或 delete year
。根据作者
来自哪里,尝试删除作者
一般来说,您不想要 delete
未分配的对象。您的 Book
类可能不合理地拥有作者
向量的所有权。
That is, you get the error when you try to delete title
or delete year
. It may also happen when trying to delete authors
depending on where authors
is coming from. In general, you don't want to delete
objects you haven't allocated. Your Book
class possibly unreasonably takes ownership of the authors
vector.
这篇关于在C ++中为类分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!