How should a number-class look like?

P

Piotr Sawuk

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);*/
}
 
B

benben

Piotr said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top