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
{
public:
	Database(char * file = "states.txt");
	~Database(void);
	void deleteDatabase ();
	void fillDatabase ();
	void insertDatabase (int index, State & tempState);
	void printDatabase ();
private:
	State * list;		// array of states
	int count;			// total count of states
	char * filename;	// input filename
};
class State
{
public:
	State::State();
//	State::State(char * name, int year, char * cap, int popRank);
	State::~State();
	void initState(char * tempName, int tempYear, char * tempCap, int tempPopRank);
	void copyState(State & tempState);
	void printState();
private:
	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;
	
	db.fillDatabase();
	db.printDatabase();

	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);
			}
			else
			{
				cout << "Error with file!" << '\n';
				return;
			}
			index++;
		}
	}
 
 	// close file
 	inputFile.close();
}
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 );
	
	return;
}

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)?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.