Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How should a number-class look like?

Reply
Thread Tools

How should a number-class look like?

 
 
Piotr Sawuk
Guest
Posts: n/a
 
      01-04-2007
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
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      01-04-2007
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);*/
> }

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      01-04-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can I use a look-ahead and a look-behind at the same time? dan.j.weber@gmail.com Perl Misc 4 04-09-2008 10:25 PM
Why do look-ahead and look-behind have to be fixed-width patterns? inhahe Python 3 01-28-2005 12:50 PM
<tr> with a 1x1 image as a filler on a table with padding of 2 look thicker in netscape but they look ok in IE. Serial # 19781010 HTML 1 08-10-2003 09:05 PM



Advertisments