C++ and derived classes issue
Geert Janssens
geert.gnucash at kobaltwit.be
Sat Feb 13 11:20:45 EST 2016
On Saturday 13 February 2016 10:10:25 Larry Evans wrote:
> On 02/12/2016 07:15 PM, John Ralls wrote:
> >> On Feb 12, 2016, at 1:09 PM, Geert Janssens
>
> <geert.gnucash at kobaltwit.be> wrote:
> >> Hi,
> >>
> >> While working to convert the csv importer to c++ I've come across
>
> this issue:
> [snip]
>
> >> To avoid code duplication, I chose to replace the struct with a
> >> small
>
> hierarchy of classes, the
>
> >> base class being GncTransProperty, which handles the common parts
> >> and
>
> derived classes for
>
> >> each type of value that can exist:
> >> * one to keep track of an account (Account *),
> >> * one to keep track of strings (std::string),
> >> * one to store a time value (time64)
> >> * and lastly one to keep an amount (gnc_numeric).
> >>
> >> I also chose a generic base class to allow the instances to be
> >> stored
>
> in a std::vector. Which
>
> >> properties get stored will depend on the user choices in the GUI.
> >> (I
>
> say "will" as that part is not
>
> >> set up just yet).
> >>
> >> Current attempt of the code is here
>
> https://github.com/gjanssens/gnucash/blob/cpp/src/import-export/csv-im
> p/gnc-trans-props.cpp
> >> and
>
> https://github.com/gjanssens/gnucash/blob/cpp/src/import-export/csv-im
> p/gnc-trans-props.hpp
>
> [snip]
>
> > I think that what you really want to do is
> >
> > template <typename T> class GncTransProperty {
> >
> > public:
> > GncTransProperty (T x, GncTransPropType p) : value{x}, property{p}
> > {}; T get_value() const { return value; }
> > GncTransPropType get_property() const { return property; }
> >
> > private:
> > T value;
> > GncTransPropType property;
> >
> > }
>
> [snip]
> Wouldn't code here:
>
> https://github.com/gjanssens/gnucash/blob/cpp/src/import-export/csv-im
> p/gnc-trans-props.hpp#L177
>
> also have to be changed? Currently, it has:
>
> /** A struct containing TransProperties that all describe a single
> transaction. */
> class GncTransPropertyList : public std::vector<GncTransProperty>
> {
> ...
> };
>
> But std::vector<GncTransProperty> wouldn't compile since
> GncTransProperty, with your change, is now a template.
>
> Regards,
> Larry
>
You're absolutely right. I have handled this by creating a purely virtual interface class
GncTransProperty and use that as a base for a templated GncTransImpl implementation class.
The std::vector is now defined as
std::vector<std::unique_ptr<GncTransProperty>>
See current code on the same url (still not all issues have been dealt with though).
Geert
More information about the gnucash-devel
mailing list