Extending moneypunct by derivation

It is easy enough to derive from moneypunct and override the virtual functions in a portable manner. But moneypunct also has a non-standard protected interface that you can take advantage of if you wish. There are nine protected data members:

  charT       __decimal_point_;
  charT       __thousands_sep_;
  string      __grouping_;
  string_type __cur_symbol_;
  string_type __positive_sign_;
  string_type __negative_sign_;
  int         __frac_digits_;
  pattern     __pos_format_;
  pattern     __neg_format_;
  

A derived class could set these data members in its constructor to whatever is appropriate, and thus not need to override the virtual methods.

Listing: Extending Moneypunct by derivation
struct mypunct
   : public std::moneypunct<char, false>

{
   mypunct();
};

mypunct::mypunct()

{

   __decimal_point_ = ',';

   __thousands_sep_ = ' ';

   __cur_symbol_ = "kr";

   __pos_format_.field[0] = __neg_format_.field[0] = char(sign);

   __pos_format_.field[1] = __neg_format_.field[1] = char(value);

   __pos_format_.field[2] = __neg_format_.field[2] = char(space);

   __pos_format_.field[3] = __neg_format_.field[3] = char(symbol);

}

int

main()

{
   std::locale loc(std::locale(), new mypunct);

   std::cout.imbue(loc);

   // ...
}

Indeed, this is just what moneypunct_byname does after reading the appropriate data from a locale data file.