Can I have std::vector store these subclasses mixed?

From: Eric Lilja (ericliljaNoSpam_at_yahoo.com)
Date: 02/26/05


Date: Sat, 26 Feb 2005 10:35:28 +0100

Hello, in my program I am reading a text file that has format:
option=value
There are several types of options and they expect values of different types
(mostly strings or integers). I made a templated base class Option and had
my different options derive from Option instantiated for their type (i.e.,
class LongOption inherits publicly from Option<long>). I selected this
approach because I wanted a uniform interface for obtaining the type and
value of a given option. I came up with the following class tree for testing
purposes:
template <typename T>
class Option
{
public:
   enum OptionType
   {
      LONG,
      STRING
   };

   Option(const std::string& name)
      :
      m_name(name),
      m_not_set(true) {}

   virtual ~Option() {}

   T get_value() const
   {
      if(m_not_set)
         throw;

      return m_value;
   }

   void set_value(const T& value)
   {
      m_not_set = false;

      m_value = value;
   }

   virtual OptionType get_type() const = 0;

private:
   std::string m_name;
   bool m_not_set;
   T m_value;
};

class LongOption : public Option<long>
{
public:
   LongOption(const std::string& name)
      :
      Option<long>(name) {}

   virtual OptionType get_type() const
   {
      return STRING;
   }
};

class StringOption : public Option<std::string>
{
public:
   StringOption(const std::string& name)
      :
      Option<std::string>(name) {}

   virtual OptionType get_type() const
   {
      return LONG;
   }
};

That seems to compile, but I cannot do:
std::vector<Option*> options(11);
options[0] = new StringOption("Name=");
The compiler complains:
$ make gccmain
g++ gccmain.cpp -o gccmain
gccmain.cpp: In function `int main()':
gccmain.cpp:6: error: use of class template `template<class T> class Option'
as expression
gccmain.cpp:6: error: parse error before `>' token
gccmain.cpp:8: error: `options' undeclared (first use this function)
gccmain.cpp:8: error: (Each undeclared identifier is reported only once for
   each function it appears in.)
make: *** [gccmain] Error 1

Not being able to store my different options in the same container makes
reading the file with options a lot messier...are their any ways to solve
this? I'm not very good at templates so I've certainly made a number of
silly mistakes.

/ Eric



Relevant Pages

  • Re: SNe1a data
    ... It also ignores the K correction. ... out gave a better chi^2 result for the dilated template so I`ll ... In other words if one were to extrapolate the last HST reading ...
    (sci.astro)
  • Re: Where can I get a template that looks like a storybook?
    ... Or use the Image Importer Wizard ... MOS Master Instructor ... next slide it appears you are turning the page to begin reading the story. ... Is there anywhere I can get this template made for me or is it ...
    (microsoft.public.powerpoint)
  • Re: [PHP] free allocated memory: HOW ?
    ... first cleans the statistics data, the second generates the new data and ... Reading in the template should only happen once and take roughly as much memory as the file is big. ...
    (php.general)
  • Re: [PHP] free allocated memory: HOW ?
    ... I am generating mails based on a HTML Template. ... are you reading the memory consumption ... each loop by approx 30 MB. ...
    (php.general)