Re: Oddity when inheriting Gmp.mpz
Arne Goedeke <[email protected]>
| Newsgroups | gmane.comp.lang.pike.user |
|---|---|
| Message-ID | <[email protected]> |
Why is cast in Gmp.mpz private anyway?
arne
On 10/27/14 06:27, Chris Angelico wrote:
> Test case:
>
> string format_time(int t) {return
> sprintf("%02d:%02d:%02d",t/3600,(t/60)%60,t%60);}
>
> class Time1
> {
> inherit Gmp.mpz;
> string _sprintf(int type,mapping|void params) {int
> val=(int)mpz::this; write("-- %d\n",val); return type=='O' ?
> format_time(val) : ::_sprintf(type,params);}
> }
>
> class Time2
> {
> inherit Gmp.mpz;
> string _sprintf(int type,mapping|void params) {int
> val=(int)mpz::this; write("-- %d\n",val); return type=='O' ?
> (string)val : ::_sprintf(type,params);}
> }
>
> int main()
> {
> write("Bad: %O\n",Time1(1234));
> write("Good: %O\n",Time2(1234));
> }
>
> I asked about this a while ago, and was advised to clean out heaps of
> my code and replace it with simple inheritance from Gmp.mpz. This
> works nicely in a simple case, but fails if I'm calling a function.
>
> Am I going about this wrongly? All I need to do is retrieve the actual
> integer stored in the current object, but simply (int)this isn't
> working ("no cast method in object"). Using (int)mpz::this does give
> correct results, but only as long as I don't pass it to another
> function. (In the actual code, format_time() is a bit more
> complicated, so simply inlining the sprintf into the time class isn't
> ideal.)
>
> It may be an over-eager optimization, but it's also likely that I'm
> completely misunderstanding something here.
>
> ChrisA
>