473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Returning bool from long integer class

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?
Jul 22 '05 #1
3 2588

"Pierre Espenan" <wh**************@yahoo.com> skrev i en meddelelse
news:91*******************@newsread1.news.pas.eart hlink.net...
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?


First, why don't you write your operator as

operator bool()const{ return *this!=0; }

this is more concise.

Second, your class does not permit you to compare your long_int with
ordinary integers. To do so, your class needs a constructor long_int(int i)
which is not explicit.

Third, your comparison functions should really be friends - as in
friend bool operator<(const long_int& lhs,const long_int& rhs);
this will enable you to write code such as:

int i;
long_int j;
....
if (i > j) ....

Fourth, there are problems with bool that suggest you better dont provide
such an operator. Instead use a conversion to void* const:

operator void* const(){ return *this!=0 ? this:0; }

This enables you to write

long_int j(0);
....
if (j) ...

Last, I do not see why you choose to expose your representation of radix to
the enduser. I would have opted for a radix-free long_int representation,
choosing a radix only when when outputting or inputting the numbers.

Kind regards
Peter
Jul 22 '05 #2
Peter Koch Larsen wrote:
"Pierre Espenan" <wh**************@yahoo.com> skrev i en meddelelse
news:91*******************@newsread1.news.pas.eart hlink.net...
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?

First, why don't you write your operator as

operator bool()const{ return *this!=0; }

this is more concise.


Right on.
Second, your class does not permit you to compare your long_int with
ordinary integers. To do so, your class needs a constructor long_int(int i)
which is not explicit.

Third, your comparison functions should really be friends - as in
Or, since I personally don't like friend functions, just make sure your
class provides enough public accessors for the comparisons to be
performed by functions outside the class.
friend bool operator<(const long_int& lhs,const long_int& rhs);
this will enable you to write code such as:

int i;
long_int j;
...
if (i > j) ....

Fourth, there are problems with bool that suggest you better dont provide
such an operator. Instead use a conversion to void* const:

operator void* const(){ return *this!=0 ? this:0; }

This enables you to write

long_int j(0);
...
if (j) ...
That's a fantastic idea I will be stealing for my own code. :)
Last, I do not see why you choose to expose your representation of radix to
the enduser. I would have opted for a radix-free long_int representation,
choosing a radix only when when outputting or inputting the numbers.

Kind regards
Peter


Jul 22 '05 #3

"Peter Koch Larsen" <pk*@mailme.dk> wrote in message
news:3f**********************@dread14.news.tele.dk ...

"Pierre Espenan" <wh**************@yahoo.com> skrev i en meddelelse
news:91*******************@newsread1.news.pas.eart hlink.net...
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?


First, why don't you write your operator as

operator bool()const{ return *this!=0; }

Thanks, I did but I had to do it like this:
operator bool()const{ return *this!=long_int(0); }
even after I took the explicit qualifier off the integer ctor

this is more concise.

Second, your class does not permit you to compare your long_int with
ordinary integers. To do so, your class needs a constructor long_int(int i) which is not explicit. I took off the explicit
Third, your comparison functions should really be friends - as in
friend bool operator<(const long_int& lhs,const long_int& rhs);
this will enable you to write code such as:

int i;
long_int j;
...
if (i > j) ....
I didn't do this yet but I'll look into it.
Fourth, there are problems with bool that suggest you better dont provide
such an operator. Instead use a conversion to void* const:

operator void* const(){ return *this!=0 ? this:0; } I had to do it the same way:
operator void* const(){ return *this!=long_int(0) ? this:0; }
to get it to work.

This enables you to write

long_int j(0);
...
if (j) ...

Last, I do not see why you choose to expose your representation of radix to the enduser. I would have opted for a radix-free long_int representation,
choosing a radix only when when outputting or inputting the numbers.
I want the user to be able to change the radix.
The default radix is decimal. The overloaded cin operator takes the radix
of the long_int into account when inputting.
The radix can be converted between all four bases, for example:
100!=933262154439441526816992388562667004907159682 64381621468592963895217599
993229915
60894146397615651828625369792082722375825118521091 68640000000000000000000000
00 (DEC) =
1B30964EC395DC24069528D54BBDA40D16E966EF9A70EB21B5 B2943A321CDF10391745570CCA
9420
C6ECB3B72ED2EE8B02EA2735C61A0000000000000000000000 00 (HEX)

Kind regards
Peter

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mchoya | last post by:
I'm so frustrated. I'm beginning school next week and I have been working on a simple program for several days now without being able to complete it. (Though I have brushed up on a lot of C++...
19
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or...
2
by: Jon Shemitz | last post by:
How come I can write code like "if (L)" but NOT code like "if (L == true)" when L is a class with an implicit operator bool? /////////// List L = new List(); public class List { private...
1
by: Bern McCarty | last post by:
What do you make of this? I cannot tell for sure but it almost seems as the the transition thunk to get back from the native bool method to the managed caller is looking at eax and, if any bit is...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
8
by: hurry | last post by:
hi, I am writing a c program in VC++ 6. I have 2 files with 3 functions. file-1 having two functions "a" and "c" file-2 having a single function "b" with function "a" as main() , "a"...
2
by: Asim Qazi | last post by:
Hi All public class MyResponse { public bool m_bStatus; public string m_szErrorCode; public string m_szMessage; }
4
by: scparker | last post by:
Hello, We have a stored procedure that does a basic insert of values. I am then able to retrieve the ID number created for this new record. We are currently using ASP.NET 2.0 and use N-Tier...
11
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have the method below that returns a bool, true or false depending on if the conversion to date tiem works. It takes a string input. I am only returning the bool but would also like to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.