So in my class we just finished covering the topic of constructors and destructors. Now, my teacher has given us a lab where we have to create these classes and each of them must have a ctor/dtor. The problem I'm running into is..well.. it would be easier to describe in code :mrgreen:

here's what my main looks like:

class Database
	Database(char * file = "states.txt");
	void deleteDatabase ();
	void fillDatabase ();
	void insertDatabase (int index, State & tempState);
	void printDatabase ();
	State * list;		// array of states
	int count;			// total count of states
	char * filename;	// input filename
class State
//	State::State(char * name, int year, char * cap, int popRank);
	void initState(char * tempName, int tempYear, char * tempCap, int tempPopRank);
	void copyState(State & tempState);
	void printState();
	char * name;	// state name
	int year;		// year of entry into the union
	char * cap;		// state capital
	int popRank;	// population ranking
int main(void)

	Database db;

	return 0;

and here's the fillDatabase function:

void Database::fillDatabase ()
	char name[80];	// temp variables
	int year;		// to store
	char cap[80];	// input data
	int popRank;		// from file
	int index = 0;	// index into array of State structs
	char array[100]; // temporary storage for file input
	State tempState;

	// open file from filename field of db
	// don't need to check for file open success because initDatabase already checked
	ifstream inputFile( filename );
	// loop through all the states
	for ( int cnt = 0; cnt < count; cnt++ )
	    // read and parse one line of file into 4 input data fields
		if ( !inputFile.eof() )
			inputFile.getline( array, 100 );

			if ( sscanf( array, "%[^,], %d, %[^,], %d",	name, &year, cap, 
				&popRank ) == 4 )
				// if tempState is created successfully from the 4 input data
//				State tempState( name, year, cap, popRank );
				tempState.initState(name, year, cap, popRank);
				insertDatabase (index, tempState);
				cout << "Error with file!" << '\n';
 	// close file
void Database::insertDatabase (int index, State & tempState)
   // set pCurrent to location to be inserted in array
	State * pCurrent = list + index;
	// copy from state to pCurrent
	pCurrent->copyState( tempState );

In this function i'm creating an instance of the tempState class to temporarily store data from the file and then copy the data into the db instance. The problem is that as soon as I leave this function, the destructor is invoked and deletes the last piece of data that the list was pointing to. How would I circumvent this problem (other than not creating a destructor)?

10 Years
Discussion Span
Last Post by degamer106

1. The problem is that, just like all other objects, tempState is destroyed when it goes out of scope and fillDatabase() runction returns to whoever called it. If you do not expicitely code the destructor c++ will write one for you.

Why do you want tempState to live outside the function in which it is defined? If it is necessary, then I would either (1) add it as a member object of the class, or (2) pass a reference to the object as a parameter to fillDatabase().


well thanks for the reply. I think I figured out the problem over lunch lol.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.