C++ and derived classes issue

Larry Evans cppljevans at suddenlink.net
Sat Feb 13 11:10:25 EST 2016


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-imp/gnc-trans-props.cpp
>> and
>>
https://github.com/gjanssens/gnucash/blob/cpp/src/import-export/csv-imp/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-imp/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




More information about the gnucash-devel mailing list