if you absolutely need a base class Chart (with virtual functions), a hack would be to overload the AddElement function.
i would prefer using templates for your containers as suggested by Narue in the other thread. and as for this issue:
> ... generic template container and have a method addelement(T& elt)
> but the implemebtation of addelement() depends on the element being added
template specialization would address the problem.
#include <stdexcept>
struct DataElement { /* ... */ };
struct AverageElement { /* ... */ };
#ifdef INHERIT
struct Chart // base class
{
virtual void AddElement( const DataElement& ) = 0 ;
virtual void AddElement( const AverageElement& ) = 0 ;
// other (pure) virtual functions
};
struct DataChart : Chart
{
virtual void AddElement( const DataElement& )
{ /* add DataElement */ }
virtual void AddElement( const AverageElement& )
{ throw std::logic_error("incorrect element type") ; }
// other overrides etc.
};
struct AveragesChart : Chart
{
virtual void AddElement( const DataElement& )
{ throw std::logic_error("incorrect element type") ; }
virtual void AddElement( const AverageElement& )
{ /* add AverageElement */ }
// other overrides etc.
};
#else // GENERIC
template< typename T > struct chart_base
{
// common stuff goes here
};
template< typename T > struct chart ; // generalization
// specialization
template<> struct chart<DataElement> : chart_base<DataElement>
{
void addelement( const DataElement& ) ;
// other DataChart specific stuff
};
// specialization
template<> struct chart<AverageElement> : chart_base<AverageElement>
{
void AddElement( const AverageElement& ) ;
// other AverageChart specific stuff
};
typedef chart<DataElement> DataChart ;
typedef chart<AverageElement> AverageChart ;
#endif // INHERIT
> I suppose one could again use a visitor here, or pass function objects
> to the charts containing the algorithm to be computed on the elements.
i guess, vistor if your chart is object-oriented, function object if it is generic.
> Which class do you think should have the responsibility of generating
> an AverageElement for the longer time frame Chart
> the ChartSet object or the Charts themselves?
perhaps an element of personal taste here. i think i would let the Charts themselves generate this if all the required information is available within a single Chart; the ChartSet otherwise.