AM> ostream& operator<<(ostream& Out, Roman& R)
AM> {
AM> Out << R.str;
AM> return Out;
AM> }
DM>You may want to hide both str and n and provide
DM>operator char* and operator
DM>int instead...
AM> I'm new to C++ (but experienced with C; a consequence of working
AM> for years in a VAX/VMS that has C but doesn't have C++). How can
AM> this be done? Overload the cast operator?
operator char*()
operator int
Normally I recommend avoiding these types of conversions. However, in a
RomanNumeral class, they actually make sense. They really ARE two different
representations of the same thing!
DM>What happens if you already have a Roman string
DM>created? You lose memory...
DM>try deallocating the string, first.
AM> But since I free the str at the destruction, isn't memory recovered?
Not in this case. You have str pointing to valid memory, and then you point
it at other valid memory, without deallocating the first valid memory. You
free str at the destruction freeing up the LAST allocated memory, but nothing
before that.
DM>Better yet, use the string class rather
DM>than the char* with strdup.
AM> Hmmm... Yes, I have to forget some C to learn C++ :-)
Something like that, yes. :->
DM>Yeah, I was worried about that. First of all, you
DM>have a str[1000] that's
DM>never used in the .cpp portion. :-)
AM> Old nasty habbit :-)
Yeah, at least compilers catch these. :->
DM>Second, a few other test cases:
DM>IL = 49 [core dumped!]
DM>IIC = 98
DM>VC = 95
AM> Those numbers are illegal. I didn't check for illegality
They aren't illegal, though, AFAIK. I could be wrong.
DM>And, the slightly more obscure:
DM>IVC = 96
DM>These are a few that you fail on. Your algorithm,
AM> I'm guessing, would have
DM>to be completely reworked to accomodate these.
DM>Especially if you want to no
DM>allow, say:
DM>IVCV ?? 101
[This one IS illegal]
AM> But all those numbers are non-roman. I guess I just have to add
AM> something that says that if there is any character left after
AM> parsing the input string, then the number is not valid.
That is pretty trivial. :->
--- Maximus/2 3.01
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)
|