simple class question

Started by
6 comments, last by Big Sassy 22 years, 6 months ago
I have a class inside of a class. I want to initialize the inside object with my own value. ex:
  
class CPig
{
private:
   
   CPhrase phrase("That''s some pig");

// other stuff

}
  
At the moment I get an error for doing that (I guess becuase you can''t initialize anything in a class). Is there anyway around this? I don''t want to use the default constructor for the object inside (ex: CPhrase phrase; ). Any help would be very much appreciated.
Advertisement
Use the CPig Destuctor/Destructor to build and delete your internal objects.

  class CPig{ public: CPig() {   phrase = new CPhrase("That''s some pig"); } ~CPig() {   delete phrase;  } private:  CPhrase *phrase};  


-------
Andrew

If you don't want to use a pointer, the way to do it is to use an initializer-list on the constructor for CPig.

   class CPig{   private:      // Don't specify any constructor-call on the declaration:      CPhrase phrase;     public:      // A public constructor for pig,      // initializing 'phrase' correctly      CPig() : phrase("That's some pig")      {         // construct other stuff here      };  };    

[EDIT: Corrected the code]

Edited by - Dactylos on September 22, 2001 7:42:49 PM
Ok. I don''t know why I didn''t think of that. Brain fart I guess. But I have another problem. Say I want to have that inside object as a static variable that all object will use. ex:

  class CPig{ public:    CPig()   {      phrase = new CPhrase("That''s some pig");    }   ~CPig()   {      delete phrase;   }private:   static CPhrase *phrase;}  


Whenever a new object would be made it would make a new CPhrase, leaving the old one on the heap. Obviously that doesn''t work well. So what would be a good way to initialize it once? Should I set a flag when it''s initialized? Is there a better way that I''m not thinking about?
I posted that last post before your post Dactylos. That would work great. I''m new to OO design, so bear with me Thank you for the reply, but I''d still like to know a good solution to the last problem. Once again, any help is much appreciated.

    class CPig{ public:    CPig()   {   }   ~CPig()   {   }private:   static CPhrase phrase;};//Initialize static member.CPhrase CPig::phrase("That''s some pig.");  





-------
Andrew

You should initialize static class members as you would a regular (non-class) variable. I'm not using a pointer in my example, since it will make things easier to 'destruct'.

      // In file: CPig.hclass CPig{   public:      CPig()      {          // construction of non-static class data here      }       ~CPig()      {         // destruction of non-static class data here      }private:      static CPhrase phrase;}    // In file: CPig.cpp  #include <CPig.h>  CPhrase CPig::phrase("This is a static pig-phrase");      

<EDIT>
Ok, acraig, you posted while I was typing
I would just like to point out that the definition of the static variable (i.e. where you give it a value) should not reside in a header. It should (like any data-definition) be defined in exactly *one* source file to avoid linker conflicts.
</EDIT>


Edited by - Dactylos on September 22, 2001 8:11:31 PM
acraig, you are the man


EDIT - whoops, posted right after you again. Thanks to you too Dactylos.

Edited by - Big Sassy on September 22, 2001 8:10:36 PM

This topic is closed to new replies.

Advertisement