Jump to content
  • Advertisement
Sign in to follow this  
Storyyeller

static lookup table

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

Suppose I have a boost::bimap of data that I want to use in some classes. Where would be the best place to declare and initialize it? Should I make it static? Could inheritance help? The data will always be the same, so the map only needs to be filled once. (Specifically, it is a list of all 564 possible arrangements of two kings in chess.)

Share this post


Link to post
Share on other sites
Advertisement
I'd pass a reference to it to every class that wants to use it when that class is created, i.e.:


class MyClass
{
boost::bitmap& kingsTable;

public:
MyClass(boost::bitmap& _kingsTable) : kingsTable(_kingsTable)
{
/* ... other constructor stuff ... */
}
};


Then you could just create it as a local or instance variable inside whichever function or object is responsible for creating all these other class instances.

Share this post


Link to post
Share on other sites
Quote:

Then you could just create it as a local or instance variable inside whichever function or object is responsible for creating all these other class instances.


Maybe I'm thinking nonsense cause I haven't had lunch yet, but...
Wouldn't it be a problem if you passed a reference to a local which would then go out of scope?

Share this post


Link to post
Share on other sites
Quote:
Original post by Morrandir
Quote:

Then you could just create it as a local or instance variable inside whichever function or object is responsible for creating all these other class instances.

Maybe I'm thinking nonsense cause I haven't had lunch yet, but...
Wouldn't it be a problem if you passed a reference to a local which would then go out of scope?
Thus the function must run the whole time program does - such as the main function [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by Morrandir
Quote:

Then you could just create it as a local or instance variable inside whichever function or object is responsible for creating all these other class instances.

Maybe I'm thinking nonsense cause I haven't had lunch yet, but...
Wouldn't it be a problem if you passed a reference to a local which would then go out of scope?
Thus the function must run the whole time program does - such as the main function [wink]

Yeah, that makes sense, just thought it wasn't really clear from your post. But maybe it was just me :)

Share this post


Link to post
Share on other sites
Having references as class members is often tricky. Consider using pointers instead. You would still make the instance as a local in main(), and pass it to object constructors by reference, but the objects would take the address of the passed-in resource, and store that pointer instead of initializing a reference. (Don't 'delete' the pointer in the object destructor! You didn't 'new' anything, after all.)

This way, you'll still be able to copy the objects. The default copy constructor will make a copy of the pointer, which is fine, because this time you don't want to copy the underlying object.

Share this post


Link to post
Share on other sites
Quote:
Original post by Storyyeller
Suppose I have a boost::bimap of data that I want to use in some classes. Where would be the best place to declare and initialize it?

In constructor of enclosing class.
struct Lookup {
Lookup()
private:
boost::bimap ....
};


Quote:
Should I make it static?

You could.

Personally, I would probably just contain the Lookup instance in whichever class would be solving the problem. So something like this:
class Solver {
Solver();
solve();
fail();
etc();
private:
Lookup lookup;
// or
// static Lookup lookup;
State state;
Foo foo;
....
};
It solver were reused, lookup could be made static inside that class.

I prefer to avoid having statics defined externally due to various C++ pitfalls.

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!