Boost logo

Boost :

From: Maciej Sobczak (maciej_at_[hidden])
Date: 2002-10-24 02:21:49


Hi Boosters,

Rémi Chateauneu wrote:
> /* A simple class for processing roman numbers */

The mathematics is the same in ancient Rome, in Warsaw and in Tokyo,
even if the numbers *look* differently.

I think that the representation of the class should be separate from the
class. You can achieve this with either separate translator functions
(see the reply from Volodya) or by wrapping integer into some class:

int i = ...;
cout << MyNumber(i);

MyNumber n;
cin >> n;
int i = n.get();

and use separate inserters/extractors like:

ostream & operator<<(ostream &os, MyNumber n) {...}
istream & operator>>(istream &is, MyNumber &n) {...}

(this is simplified, of course - in production code there's a lot more
to this)

This way, you can write functions like:

void fun(ostream &os)
{
        os << "hello " << MyNumber(10) << endl;
}

and later switch between different representations *externally* to the
function, possibly using my stream state wrappers. :)

Funny thing - I've just went through this discussion with people on my
univ. The Roman number class was a homework, probably well-intentioned
but IMHO very misguided.

Cheers,

---
Maciej Sobczak
http://www.maciejsobczak.com/

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk