ctor/dtor problem

Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

ctor/dtor problem

 
0
  #1
Feb 13th, 2007
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:

  1. class Database
  2. {
  3. public:
  4. Database(char * file = "states.txt");
  5. ~Database(void);
  6. void deleteDatabase ();
  7. void fillDatabase ();
  8. void insertDatabase (int index, State & tempState);
  9. void printDatabase ();
  10. private:
  11. State * list; // array of states
  12. int count; // total count of states
  13. char * filename; // input filename
  14. };

  1. class State
  2. {
  3. public:
  4. State::State();
  5. // State::State(char * name, int year, char * cap, int popRank);
  6. State::~State();
  7. void initState(char * tempName, int tempYear, char * tempCap, int tempPopRank);
  8. void copyState(State & tempState);
  9. void printState();
  10. private:
  11. char * name; // state name
  12. int year; // year of entry into the union
  13. char * cap; // state capital
  14. int popRank; // population ranking
  15. };

  1. int main(void)
  2. {
  3.  
  4. Database db;
  5.  
  6. db.fillDatabase();
  7. db.printDatabase();
  8.  
  9. return 0;
  10. }

and here's the fillDatabase function:

  1. void Database::fillDatabase ()
  2. {
  3. char name[80]; // temp variables
  4. int year; // to store
  5. char cap[80]; // input data
  6. int popRank; // from file
  7. int index = 0; // index into array of State structs
  8. char array[100]; // temporary storage for file input
  9. State tempState;
  10.  
  11. // open file from filename field of db
  12. // don't need to check for file open success because initDatabase already checked
  13. ifstream inputFile( filename );
  14.  
  15. // loop through all the states
  16. for ( int cnt = 0; cnt < count; cnt++ )
  17. {
  18. // read and parse one line of file into 4 input data fields
  19. if ( !inputFile.eof() )
  20. {
  21. inputFile.getline( array, 100 );
  22.  
  23. if ( sscanf( array, "%[^,], %d, %[^,], %d", name, &year, cap,
  24. &popRank ) == 4 )
  25. {
  26. // if tempState is created successfully from the 4 input data
  27. // State tempState( name, year, cap, popRank );
  28. tempState.initState(name, year, cap, popRank);
  29. insertDatabase (index, tempState);
  30. }
  31. else
  32. {
  33. cout << "Error with file!" << '\n';
  34. return;
  35. }
  36. index++;
  37. }
  38. }
  39.  
  40. // close file
  41. inputFile.close();
  42. }
  1. void Database::insertDatabase (int index, State & tempState)
  2. {
  3. // set pCurrent to location to be inserted in array
  4. State * pCurrent = list + index;
  5. // copy from state to pCurrent
  6. pCurrent->copyState( tempState );
  7.  
  8. return;
  9. }

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)?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,161
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1438
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: ctor/dtor problem

 
0
  #2
Feb 13th, 2007
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().
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: ctor/dtor problem

 
0
  #3
Feb 13th, 2007
well thanks for the reply. I think I figured out the problem over lunch lol.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC