Jump to content
  • Advertisement
Sign in to follow this  
Master Jake

Initializing Static Object Member (Constructor)

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

In a class, when you declare a variable of type "static," you must initialize it before use. Say, for instance, that the particular variable is an instance of another class though. How would you initialize statically?

So far, I've noticed that when you create a static array of objects, that their constructors are automatically called.

Example:

class A
{
private:
static MyObject obj[];
};

MyObject A::obj[1]; // creates 1 instance of MyObject and calls its constructor


Is there another way to do this without arrays?

Share this post


Link to post
Share on other sites
Advertisement
Like this:

class A
{
private:
static MyObject obj;
};

MyObject A::obj(arg1, arg2, arg3);

// or if you don't need arguments for constructor:
// MyObject A::obj;

Share this post


Link to post
Share on other sites

In a class, when you declare a variable of type "static," you must initialize it before use.

This is of course true with any variable, not just statics, of course.


Say, for instance, that the particular variable is an instance of another class though. How would you initialize statically?
[/quote]


#include <string>
#include <iostream>

struct A { static std::string messages[3]; };

std::string A::messages[3] = {
std::string("hello"), // Don't really need to explicitly call string
std::string("goodbye"), // ctor here, could use conversion from const char*.
std::string("thanks for all the fish") // For illustration purposes.
};

int main()
{
for (int i = 0; i != sizeof A::messages / sizeof *A::messages; ++i)
std::cout << A::messages << '\n';

return 0;
}



So far, I've noticed that when you create a static array of objects, that their constructors are automatically called.
[/quote]
They have to be initialized somehow and the point of constructors is to initialize, so this seems entirely reasonable :)


Is there another way to do this without arrays?
[/quote]

Do what without arrays?

Share this post


Link to post
Share on other sites

Like this:

class A
{
private:
static MyObject obj;
};

MyObject A::obj(arg1, arg2, arg3);

// or if you don't need arguments for constructor:
// MyObject A::obj;



Thank you very much! It seems so obvious now that I feel somewhat stupid.

Thanks for your reply too edd, but Martin's example pretty much takes care of what I want to do.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!