Jump to content
  • Advertisement

Archived

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

simple class question

This topic is 6085 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

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

    
class CPig
{
public:
CPig()
{
}

~CPig()
{
}
private:
static CPhrase phrase;
};

//Initialize static member.

CPhrase CPig::phrase("That''s some pig.");






-------
Andrew

Share this post


Link to post
Share on other sites
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.h

class 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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!