943,942 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4917
  • C++ RSS
Feb 13th, 2007
0

ctor/dtor problem

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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. };

C++ Syntax (Toggle Plain Text)
  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. };

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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. }
C++ Syntax (Toggle Plain Text)
  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)?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Feb 13th, 2007
0

Re: ctor/dtor problem

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().
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 13th, 2007
0

Re: ctor/dtor problem

well thanks for the reply. I think I figured out the problem over lunch lol.
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Creating a Basic String Database
Next Thread in C++ Forum Timeline: adding library





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC