• Advertisement

Archived

This topic is now archived and is closed to further replies.

another simple class question

This topic is 5994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

You all were so helpful last time, I have another question. lets say you have a class with a static pointer you initialize in the .cpp file. ex:
  
// header file (cpig.h)

class CPhrase
{
private:
   char* phrase;

public:
   CPhrase(char* string = "zuckerman''s famous pig")
   {
      phrase = string;
   }
}

class CPig
{
private:
   static CPhrase* phrase;

public
   CPig();
   ~CPig();

   void Out();
}

// implementation file (cpig.cpp)

CPhrase* Cpig::phrase = new CPhrase("that''s some pig");

// constructor and stuff...

  
Now lets say I make a bunch of CPig classes. When the program terminates and all the destructors are called, how do I free the memory allocated for the static varialbe (CPhrase* phrase)? Any help would be much apprecited

Share this post


Link to post
Share on other sites
Advertisement
That's why you probably shouldn't make it a pointer. On the other hand (though it's not 'clean'), the OS will release all dynamically allocated memory when your program exits.

In your case, there is no need to make the phrase a pointer. i.e.
     
class CPig
{
private:
static CPhrase phrase;

public:
CPig();
~CPig();

void Out();
}

// implementation file (cpig.cpp)

CPhrase Cpig::phrase("that's some pig");
// Or if the compiler thinks that the above is a function declaration, write it as follows:

CPhrase CPig::phrase = CPhrase("that's some pig"); // note: there is no 'new'



Edited by - Dactylos on September 23, 2001 7:33:57 PM

Share this post


Link to post
Share on other sites
Thanks for the reply, man. In the program I'm making I'm pretty sure I need it to be a pointer. That's my dilema. Basically, I implemented my own linked list class. I may just get rid of it and finally look into the STL. But I have a list that stores all of my sprites so when I need to restore them from an ALT-TAB I can just call the restore from the list and they all will be back. I also had other things implemented into the list, but now since I've upgraded my sprite library to OO, I have no need for them. Anyway, thanks for the help. I seriously appreciate it

[EDIT] Oh yeah, my library used to be modular in design (in case you didn't figure it out ). So now I have to clean up my sloppy code from before (creating more sloppy code apparently).

Edited by - Big Sassy on September 23, 2001 7:48:37 PM

Share this post


Link to post
Share on other sites
On a side note, initialisation in a class constructor is better done in the initialisator list:


CPhrase(char* string = "zuckerman''s famous pig"):
phrase(string) {}

Share this post


Link to post
Share on other sites

  • Advertisement