sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Piotr Sawuk's Avatar

How should a number-class look like?


Question posted by: Piotr Sawuk (Guest) on January 4th, 2007 02:05 AM
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
2 Answers Posted
red floyd's Avatar
Guest - n/a Posts
#2: Re: How should a number-class look like?

Piotr Sawuk wrote:
Quote:
Originally Posted by
What is wrong with the following? Why doesn't it compile?


What errors are you getting?
Quote:
Originally Posted by
>
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);*/
}

benben's Avatar
Guest - n/a Posts
#3: Re: How should a number-class look like?

Piotr Sawuk wrote:
Quote:
Originally Posted by
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.
Quote:
Originally Posted by
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
 
Not the answer you were looking for? Post your question . . .
197,003 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,003 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors