![]() |
| ||
| ctor/dtor problem 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 class State int main(void) and here's the fillDatabase function: void Database::fillDatabase () void Database::insertDatabase (int index, State & 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)? |
| ||
| 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(). |
| ||
| Re: ctor/dtor problem well thanks for the reply. I think I figured out the problem over lunch lol. |
| All times are GMT -4. The time now is 10:10 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC