Jump to content
  • Advertisement

Archived

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

tscott1213

Staticd v Global Object Creation

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

Hey gang, I have two questions. 1) I am using Microsoft''s VC++ compiler, is it not possible to step through code in an object''s constructor? Typically, when I am debugging my code, I run to a specific line in the code and then step through to examine variables and whatnot. However, I don''t seem to be able to do this in any of my constructor functions? 2) This one is a bit tougher (for me at least). I have an object with a static data member. The static data member is a list of pointers to all of the objects that that have been created. Let''s call this obj_w_static. I have another object that is instatiated globally. The constructor for this global object creates an obj_w_static object and calls one of its member functions. The member function attempts to use the static data member, but it appears taht the static data member has not been created yet. So two questions: a) Is what I am describing possible or have I misread the problem. b) Is there a way to force the static variable to be created first? Thanks Todd

Share this post


Link to post
Share on other sites
Advertisement
For question 1, I am not very sure but wouldn't you have to step into where you call obj = new cobj();? I am still getting the hang of the debuger myself... for question 2 I am pretty sure you have to initialize static member variables outside of the constructor kind of like as if they were a global variable that gets initialized at runtime:

    
// the class .h file

class cobj
{
static int m_iStaticInt;
...;
};

// the .cpp file

// initialize the static member outside the constructor

static int cobj::m_iStaticInt = 0;
cobj::cobj()
{
m_iStaticInt++;
// rest of object construction

...;
}

cobj::~cobj()
{
m_iStaticInt--;
// rest of object destructor

...;
}


That is how I would go about it. I could be wrong so I am going to keep an eye on this thread to find out if I am just blatantly wrong or what. Hopefully someone who knows for sure will either confirm or deny what I said.

EDIT: tags

[edited by - evillive2 on April 18, 2003 9:06:57 PM]

Share this post


Link to post
Share on other sites
for question 2, do something like this:


class Blah
{
static YourStatic * GetStatic();
...
};

//cpp file

YourStatic * Blah::GetStatic()
{
static YourStatic Tag;
return &Tag;
}

that will force YourStatic Tag to be created when its needed.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!