P
Pierre Espenan
A have a long integer class. The built integer type within a conditional
statement returns bool false for int i=0 and bool true for any other non
zero value. I want my long integer class to have similar behavior. My
class looks like this:
#ifndef long_int_H
#define long_int_H
#include <string>
using namespace std;
typedef valarray<complex<double> > VCD;
typedef valarray<complex<float> > VCF;
typedef valarray<float> VAF;
typedef complex<double> COMPLEX;
const unsigned MAX_SIZE=1048576; //2^20
enum base{BIN=2, OCT=8, DEC=10, HEX=16};
base RAD=DEC;
class long_int{
friend ostream& operator<<(ostream& , const long_int& );
friend istream& operator>>(istream& , long_int& );
private:
base _radix;
string _str ;
int _len ;
long_int x_digit(const int&)const;
void x_radix();
void carry(VAF&)const;
string put_str()const;
static char itoc(const int& );
static int ctoi(const char&);
public:
long_int(base radix=RAD):_radix(radix),_str("0"),_len(1){}
explicit long_int(const string& s, base radix=RAD);
explicit long_int(const signed long& n, base radix=RAD);
long_int& operator=(const long_int& v);
long_int& operator=(const string& s);
long_int& operator=(const signed long& n);
long_int& operator+=(const long_int&);
long_int& operator++();
long_int operator++(int);
long_int& operator-=(const long_int&);
long_int& operator--();
long_int operator--(int);
long_int& operator*=(const long_int&);
long_int& operator/=(const long_int&);
long_int& operator%=(const long_int&);
long_int operator!()const;
long_int operator-()const;
long_int operator+()const;
long_int sqrt()const;
long_int abs()const;
void radix_convert(base radix=RAD);
int size()const;
bool operator<(const long_int&)const;
bool operator>(const long_int&)const;
bool operator==(const long_int&)const;
bool operator!=(const long_int&)const;
bool operator<=(const long_int&)const;
bool operator>=(const long_int&)const;
bool is_odd()const;
};
#endif
In my attempts to implement the bool functionality if I add "operator
bool()const{ if(*this==0) return false; else return true; }" below the bool
operators the compiler gives multiple warnings with the "<", "<=" and "%"
bool operators specifically "unsafe use of type 'bool' in operation". ( If
I remove the explicit qualifier from the second constructor I get multiple
errors --- "two overloads have similar conversions".) The warnings are
occuring in conditional statements within the definition of the arithmatic
operators. Are these warnings significant? Is this the correct way to
implement bool functionality?
statement returns bool false for int i=0 and bool true for any other non
zero value. I want my long integer class to have similar behavior. My
class looks like this:
#ifndef long_int_H
#define long_int_H
#include <string>
using namespace std;
typedef valarray<complex<double> > VCD;
typedef valarray<complex<float> > VCF;
typedef valarray<float> VAF;
typedef complex<double> COMPLEX;
const unsigned MAX_SIZE=1048576; //2^20
enum base{BIN=2, OCT=8, DEC=10, HEX=16};
base RAD=DEC;
class long_int{
friend ostream& operator<<(ostream& , const long_int& );
friend istream& operator>>(istream& , long_int& );
private:
base _radix;
string _str ;
int _len ;
long_int x_digit(const int&)const;
void x_radix();
void carry(VAF&)const;
string put_str()const;
static char itoc(const int& );
static int ctoi(const char&);
public:
long_int(base radix=RAD):_radix(radix),_str("0"),_len(1){}
explicit long_int(const string& s, base radix=RAD);
explicit long_int(const signed long& n, base radix=RAD);
long_int& operator=(const long_int& v);
long_int& operator=(const string& s);
long_int& operator=(const signed long& n);
long_int& operator+=(const long_int&);
long_int& operator++();
long_int operator++(int);
long_int& operator-=(const long_int&);
long_int& operator--();
long_int operator--(int);
long_int& operator*=(const long_int&);
long_int& operator/=(const long_int&);
long_int& operator%=(const long_int&);
long_int operator!()const;
long_int operator-()const;
long_int operator+()const;
long_int sqrt()const;
long_int abs()const;
void radix_convert(base radix=RAD);
int size()const;
bool operator<(const long_int&)const;
bool operator>(const long_int&)const;
bool operator==(const long_int&)const;
bool operator!=(const long_int&)const;
bool operator<=(const long_int&)const;
bool operator>=(const long_int&)const;
bool is_odd()const;
};
#endif
In my attempts to implement the bool functionality if I add "operator
bool()const{ if(*this==0) return false; else return true; }" below the bool
operators the compiler gives multiple warnings with the "<", "<=" and "%"
bool operators specifically "unsafe use of type 'bool' in operation". ( If
I remove the explicit qualifier from the second constructor I get multiple
errors --- "two overloads have similar conversions".) The warnings are
occuring in conditional statements within the definition of the arithmatic
operators. Are these warnings significant? Is this the correct way to
implement bool functionality?