static lookup table
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'd pass a reference to it to every class that wants to use it when that class is created, i.e.:
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.
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.
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 MorrandirThus the function must run the whole time program does - such as the main function [wink]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 swiftcoderQuote:Original post by MorrandirThus the function must run the whole time program does - such as the main function [wink]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?
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.
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
Popular Topics
Advertisement