static lookup table

Started by
5 comments, last by Antheus 14 years, 11 months ago
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.)
I trust exceptions about as far as I can throw them.
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.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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?
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]

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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 :)
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.
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.

This topic is closed to new replies.

Advertisement