Boost logo

Boost Users :

From: Scott Finley (sfinley_at_[hidden])
Date: 2007-04-05 12:28:15


I am wondering about best practices for the use of pointers to static
structures.

I have a class that will essentially replace an enumeration and
represent a set of options. The options will be static members of the
class. I would also like to have a vector of pointers to all the static
members in order to be able to query all possible values at run time.
This vector would be initialized explicitly by the constructor. Is there
any reason that the vector should not hold normal raw pointers?
Obviously I want to use these options without copying them around all
over the place, so pointers seem to be the answer, but I will never be
creating an option dynamically, so I don't really have any risk of
memory leaks. Is there any reason to use shared_ptr with a custom
destructor that does nothing?

My use case would be something like:

class myOption;

class myOptionSet
{
public:
  myOptionSet()
  {
    allOptions.push_back( &OPTION_1 );
    allOptions.push_back( &OPTION_2 );
  }

  vector<myOption> const &getAllOptions() { return allOptions; }

  static myOption const OPTION_1; // initialized in .cpp
  static myOption const OPTION_2;

private:
  static vector<myOption const *> allOptions;
}

class OptionUsingClass
{
  OptionUsingClass( vector<myOption> const &possibleOptions );

  // This can now check that the option is a possibleOption
  void doSomething( myOption const &option );

  // or maybe this is better...
  void doSomething( myOption const *option );
};

Is this a good way to do things? Should I be using some sort of pointer
wrapper instead?

-- 
/**************************************
 * Scott Finley
 * Research Software Engineer
 * TomoTherapy Inc.
 * Email: sfinley_at_[hidden]
 * Phone: 608.824.3381
 ***/

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net