| | |
ctor/dtor problem
![]() |
•
•
Join Date: Mar 2006
Posts: 131
Reputation:
Solved Threads: 0
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:
and here's the fillDatabase function:
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)?
here's what my main looks like:
C++ Syntax (Toggle Plain Text)
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 };
C++ Syntax (Toggle Plain Text)
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 };
C++ Syntax (Toggle Plain Text)
int main(void) { Database db; db.fillDatabase(); db.printDatabase(); return 0; }
and here's the fillDatabase function:
C++ Syntax (Toggle Plain Text)
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(); }
C++ Syntax (Toggle Plain Text)
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().
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.
![]() |
Similar Threads
- Problem with Windows Update and WinXP (Web Browsers)
- function style cast question (C++)
- .ctor not a valid identifier (VB.NET)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Connection Problems (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: Creating a Basic String Database
- Next Thread: adding library
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






