title: C++: How to define a virtual function with generic return type?
body: <p>I'm trying to define a base a class in C++ that has pure virtual methods to be implemented by children classes.</p>
  
  <p>I would like to define setter and getter functions for basic types in the base class, but I would like the derived class to be able to determine the basic type of the getter and setter. For instance my base class would look like this:</p>
  
  <pre><code>    class Value
      {
      public:
          Value();
          template &lt;class T&gt;;
          virtual T getValue() = 0;
          virtual void setValue(T val) = 0;
      }
  </code></pre>
  
  <p>and my child class would look something like this:</p>
  
  <pre><code>    class IntValue : public Value
      {
      public:
          IntValue();
          template &lt;class T&gt;
          T getValue() {return _val};
          void setValue(T val) {_val = val};
      private:
          int _val;
      }
  </code></pre>
  
  <p>Of course the above code does not work. Any ideas on how to achieve this? Thanks in advance.</p>