Improper Destruction of Objects.

Feb 2, 2014 at 3:44pm
Hello i have two simple classes: Author and Book. Class Author contains class Book as a member. Additionally i have a list of Book objects within each Author class(P.S. this is NOT for homework, im just playing around)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Book.h
#include <iostream>
#include <string>
#include <cstring>

class Book{
private:
	char* bname;//book name
	int rldate;// release date
public:
	Book::Book(const char* aname, int adate);
	Book::~Book();
	
	void Book::Print();
	void Book::SetBook(const char*,int);

	



};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Book.cpp
#include "Book.h"
using namespace std;

Book::Book(const char* aname, int adate){
	this->bname = new char[strlen(aname) + 1];
	strcpy(bname, aname);
	rldate = adate;
	cout << " variable bname before exiting my Book constructor:" << bname << endl;
}
Book::~Book(){
	cout << "destructor of Book called" << endl;
	delete[] bname;
	
}
void Book::Print(){
	cout << "Book's name:" << bname << endl;
	cout << "Book's realese date:" << rldate << endl;
}

void Book::SetBook(const char* aname, int rldate){
	this->bname = new char[strlen(aname) + 1];

	strcpy(bname, aname);
	cout << "Book's members changed through SetBook()  with name:" << bname << endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Author.h
#include "Book.h"
#include <vector>
using namespace std;
class Author{
private:
	char* name;
	int birthdate; 
public:
	vector<Book> BookList;
	vector<Book>::iterator myIter;

	Author::Author(const char* aname,int bdate);
	Author::~Author();
	void Author::AddBook(const char* abook,int rldate);
	void Author::PrintInfo();
	void Author::PrintBooks();
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Author.cpp
#include "Author.h"

Author::Author(const char *aname, int bdate){
	this->name = new char[strlen(aname) + 1];
	birthdate = bdate;
	
	strcpy(name, aname);

}
Author::~Author(){
	cout << "destructor of author called" << endl;
	delete[] name;
	
	
}
void Author::AddBook(const char* bookname, int releasedate){
	BookList.push_back(Book(bookname, releasedate));
}
void Author::PrintInfo(){
	cout << "Author's name:" << name << endl << "Author's birth date:" << birthdate << endl;
}
void Author::PrintBooks(){
	cout << name << "'s published books:" << endl;
	cout << "book list size:" << BookList.size() << endl;
	for (myIter=BookList.begin(); myIter!=BookList.end(); myIter++){
	(*myIter).Print();
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//main.cpp
#include "Author.h"

int main(){
	Book a("A Frogs thug life", 1993);
	a.Print();
	
	Author b("Alex", 1892);
	cout << endl << endl;
	b.PrintInfo();
	cout << endl << endl;
	b.AddBook("Life on Jupiter",1991);
	b.BookList[0].Print();
	
	system("PAUSE");
	return 0;
}


//output WITHOUGHT debugging
 variable bname before exiting my Book constructor:A Frogs thug life
Book's name:A Frogs thug life
Book's realese date:1993


Author's name:Alex
Author's birth date:1892


 variable bname before exiting my Book constructor:Life on Jupiter
destructor of Book called
Book's name:▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌♀
Book's realese date:1991
Press any key to continue . . .


Basically these are my problems:
- Why is the Book's instance's destructor called right after my program exits Book's construcor
- Does Book's name(during the output of my Print() method) print these wierd symbols due to the fact that this instance of Book got destroyed?
-if this instance of Book actually got destroyed before the Print() method,why the hell is it able to print the Book's release date?
Feb 2, 2014 at 4:05pm
the vector is resizing and relocating the first book to the new storage, which calls the move/copy constructor of Book and the destructor on the old copy.

Either provide a copy constructor (see Rule of 3) or, much better, switch from pointers to actual strings (as in, std::string name;), in which case you won't even need to write a destructor.
Feb 2, 2014 at 8:17pm
thanks a tons.
Topic archived. No new replies allowed.