How should a number-class look like?

Discussion in 'C++' started by Piotr Sawuk, Jan 4, 2007.

  1. Piotr Sawuk

    Piotr Sawuk Guest

    What is wrong with the following? Why doesn't it compile?

    template<int f, class me, typename ValType=int>
    class Fm
    {
    protected:
    ValType v;
    public:
    me& operator+=(const me& o) {v=((me*)this)->operator+(o); return *(me*)this;}
    me& operator*=(const me& o) {v=((me*)this)->operator*(o); return *(me*)this;}
    me& operator-=(const me& o) {v=((me*)this)->operator-(o); return *(me*)this;}
    me& operator/=(const me& o) {v=((me*)this)->operator/(o); return *(me*)this;}
    friend me operator+(const me& a, const me& b) {return me(a.operator+(b));}
    friend me operator*(const me& a, const me& b) {return me(a.operator*(b));}
    friend me operator-(const me& a, const me& b) {return me(a.operator-(b));}
    friend me operator/(const me& a, const me& b) {return me(a.operator/(b));}
    };

    class F0m : public Fm<0,F0m>
    {
    friend class Fm<0,F0m>;
    int operator*(const F0m& o) const {return v*o.v;}
    int operator/(const F0m& o) const {return v/o.v;}
    int operator+(const F0m& o) const {return (o.v==v ? 0 : v+o.v);}
    int operator-(const F0m& o) const {return (o.v==-v ? 0 : v-o.v);}
    public:
    F0m() {}
    F0m(int n) {n%=3; v=(n>1 ? -1 : n);}
    F0m& operator=(int n) {n%=3; v=(n>1 ? -1 : n); return *this;}
    };

    int main(...){
    F0m n,p,z;
    n=-1;
    p=4;
    z=234;
    p=n*n;
    n*=p;
    z*=n;
    z=n+p;
    z=p-p;
    n=p+p;
    n+=z;
    p-=z;
    p=n/n;
    n/=p;
    /* assert(n!=p && p!=z && z!=n);*/
    }
    --
    Better send the eMails to netscape.net, as to
    evade useless burthening of my provider's /dev/null...

    P
     
    Piotr Sawuk, Jan 4, 2007
    #1
    1. Advertising

  2. Piotr Sawuk

    red floyd Guest

    Piotr Sawuk wrote:
    > What is wrong with the following? Why doesn't it compile?


    What errors are you getting?

    >
    > template<int f, class me, typename ValType=int>
    > class Fm
    > {
    > protected:
    > ValType v;
    > public:
    > me& operator+=(const me& o) {v=((me*)this)->operator+(o); return *(me*)this;}
    > me& operator*=(const me& o) {v=((me*)this)->operator*(o); return *(me*)this;}
    > me& operator-=(const me& o) {v=((me*)this)->operator-(o); return *(me*)this;}
    > me& operator/=(const me& o) {v=((me*)this)->operator/(o); return *(me*)this;}
    > friend me operator+(const me& a, const me& b) {return me(a.operator+(b));}
    > friend me operator*(const me& a, const me& b) {return me(a.operator*(b));}
    > friend me operator-(const me& a, const me& b) {return me(a.operator-(b));}
    > friend me operator/(const me& a, const me& b) {return me(a.operator/(b));}
    > };
    >
    > class F0m : public Fm<0,F0m>
    > {
    > friend class Fm<0,F0m>;
    > int operator*(const F0m& o) const {return v*o.v;}
    > int operator/(const F0m& o) const {return v/o.v;}
    > int operator+(const F0m& o) const {return (o.v==v ? 0 : v+o.v);}
    > int operator-(const F0m& o) const {return (o.v==-v ? 0 : v-o.v);}
    > public:
    > F0m() {}
    > F0m(int n) {n%=3; v=(n>1 ? -1 : n);}
    > F0m& operator=(int n) {n%=3; v=(n>1 ? -1 : n); return *this;}
    > };
    >
    > int main(...){
    > F0m n,p,z;
    > n=-1;
    > p=4;
    > z=234;
    > p=n*n;
    > n*=p;
    > z*=n;
    > z=n+p;
    > z=p-p;
    > n=p+p;
    > n+=z;
    > p-=z;
    > p=n/n;
    > n/=p;
    > /* assert(n!=p && p!=z && z!=n);*/
    > }
     
    red floyd, Jan 4, 2007
    #2
    1. Advertising

  3. Piotr Sawuk

    benben Guest

    Piotr Sawuk wrote:
    > What is wrong with the following? Why doesn't it compile?
    >
    > template<int f, class me, typename ValType=int>
    > class Fm
    > {
    > protected:
    > ValType v;
    > public:
    > me& operator+=(const me& o) {v=((me*)this)->operator+(o); return *(me*)this;}
    > me& operator*=(const me& o) {v=((me*)this)->operator*(o); return *(me*)this;}
    > me& operator-=(const me& o) {v=((me*)this)->operator-(o); return *(me*)this;}
    > me& operator/=(const me& o) {v=((me*)this)->operator/(o); return *(me*)this;}
    > friend me operator+(const me& a, const me& b) {return me(a.operator+(b));}
    > friend me operator*(const me& a, const me& b) {return me(a.operator*(b));}
    > friend me operator-(const me& a, const me& b) {return me(a.operator-(b));}
    > friend me operator/(const me& a, const me& b) {return me(a.operator/(b));}
    > };
    >
    > class F0m : public Fm<0,F0m>
    > {
    > friend class Fm<0,F0m>;
    > int operator*(const F0m& o) const {return v*o.v;}
    > int operator/(const F0m& o) const {return v/o.v;}
    > int operator+(const F0m& o) const {return (o.v==v ? 0 : v+o.v);}
    > int operator-(const F0m& o) const {return (o.v==-v ? 0 : v-o.v);}


    Remember that name look up happens before access check. Simply by making
    the above overloaded operators private doesn't mean the compile won't
    have a hard time to compare those with other candidates.

    > public:
    > F0m() {}
    > F0m(int n) {n%=3; v=(n>1 ? -1 : n);}
    > F0m& operator=(int n) {n%=3; v=(n>1 ? -1 : n); return *this;}
    > };
    >
    > int main(...){
    > F0m n,p,z;
    > n=-1;
    > p=4;
    > z=234;
    > p=n*n;
    > n*=p;
    > z*=n;
    > z=n+p;
    > z=p-p;
    > n=p+p;
    > n+=z;
    > p-=z;
    > p=n/n;
    > n/=p;
    > /* assert(n!=p && p!=z && z!=n);*/
    > }



    Regards,
    Ben
     
    benben, Jan 4, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    0
    Views:
    422
  2. inhahe
    Replies:
    3
    Views:
    2,493
    Diez B. Roggisch
    Jan 28, 2005
  3. mark4asp
    Replies:
    2
    Views:
    366
    George Ter-Saakov
    Jan 8, 2008
  4. mark4asp
    Replies:
    6
    Views:
    500
    George Ter-Saakov
    Jan 8, 2008
  5. active
    Replies:
    4
    Views:
    314
    active
    Apr 3, 2007
Loading...

Share This Page