473,606 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<comple x<double> > VCD;
typedef valarray<comple x<float> > VCF;
typedef valarray<float> VAF;
typedef complex<double> COMPLEX;
const unsigned MAX_SIZE=104857 6; //2^20
enum base{BIN=2, OCT=8, DEC=10, HEX=16};
base RAD=DEC;

class long_int{
friend ostream& operator<<(ostr eam& , const long_int& );
friend istream& operator>>(istr eam& , long_int& );
private:
base _radix;
string _str ;
int _len ;
long_int x_digit(const int&)const;
void x_radix();
void carry(VAF&)cons t;
string put_str()const;
static char itoc(const int& );
static int ctoi(const char&);
public:
long_int(base radix=RAD):_rad ix(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+=(cons t 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*=(cons t long_int&);
long_int& operator/=(const long_int&);
long_int& operator%=(cons t long_int&);
long_int operator!()cons t;
long_int operator-()const;
long_int operator+()cons t;
long_int sqrt()const;
long_int abs()const;
void radix_convert(b ase radix=RAD);
int size()const;
bool operator<(const long_int&)const ;
bool operator>(const long_int&)const ;
bool operator==(cons t long_int&)const ;
bool operator!=(cons t long_int&)const ;
bool operator<=(cons t long_int&)const ;
bool operator>=(cons t 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 2604

"Pierre Espenan" <wh************ **@yahoo.com> skrev i en meddelelse
news:91******** ***********@new sread1.news.pas .earthlink.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<comple x<double> > VCD;
typedef valarray<comple x<float> > VCF;
typedef valarray<float> VAF;
typedef complex<double> COMPLEX;
const unsigned MAX_SIZE=104857 6; //2^20
enum base{BIN=2, OCT=8, DEC=10, HEX=16};
base RAD=DEC;

class long_int{
friend ostream& operator<<(ostr eam& , const long_int& );
friend istream& operator>>(istr eam& , long_int& );
private:
base _radix;
string _str ;
int _len ;
long_int x_digit(const int&)const;
void x_radix();
void carry(VAF&)cons t;
string put_str()const;
static char itoc(const int& );
static int ctoi(const char&);
public:
long_int(base radix=RAD):_rad ix(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+=(cons t 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*=(cons t long_int&);
long_int& operator/=(const long_int&);
long_int& operator%=(cons t long_int&);
long_int operator!()cons t;
long_int operator-()const;
long_int operator+()cons t;
long_int sqrt()const;
long_int abs()const;
void radix_convert(b ase radix=RAD);
int size()const;
bool operator<(const long_int&)const ;
bool operator>(const long_int&)const ;
bool operator==(cons t long_int&)const ;
bool operator!=(cons t long_int&)const ;
bool operator<=(cons t long_int&)const ;
bool operator>=(cons t 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******** ***********@new sread1.news.pas .earthlink.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<comple x<double> > VCD;
typedef valarray<comple x<float> > VCF;
typedef valarray<float> VAF;
typedef complex<double> COMPLEX;
const unsigned MAX_SIZE=104857 6; //2^20
enum base{BIN=2, OCT=8, DEC=10, HEX=16};
base RAD=DEC;

class long_int{
friend ostream& operator<<(ostr eam& , const long_int& );
friend istream& operator>>(istr eam& , long_int& );
private:
base _radix;
string _str ;
int _len ;
long_int x_digit(const int&)const;
void x_radix();
void carry(VAF&)cons t;
string put_str()const;
static char itoc(const int& );
static int ctoi(const char&);
public:
long_int(base radix=RAD):_rad ix(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+=(cons t 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*=(cons t long_int&);
long_int& operator/=(const long_int&);
long_int& operator%=(cons t long_int&);
long_int operator!()cons t;
long_int operator-()const;
long_int operator+()cons t;
long_int sqrt()const;
long_int abs()const;
void radix_convert(b ase radix=RAD);
int size()const;
bool operator<(const long_int&)const ;
bool operator>(const long_int&)const ;
bool operator==(cons t long_int&)const ;
bool operator!=(cons t long_int&)const ;
bool operator<=(cons t long_int&)const ;
bool operator>=(cons t 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.d k> wrote in message
news:3f******** **************@ dread14.news.te le.dk...

"Pierre Espenan" <wh************ **@yahoo.com> skrev i en meddelelse
news:91******** ***********@new sread1.news.pas .earthlink.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<comple x<double> > VCD;
typedef valarray<comple x<float> > VCF;
typedef valarray<float> VAF;
typedef complex<double> COMPLEX;
const unsigned MAX_SIZE=104857 6; //2^20
enum base{BIN=2, OCT=8, DEC=10, HEX=16};
base RAD=DEC;

class long_int{
friend ostream& operator<<(ostr eam& , const long_int& );
friend istream& operator>>(istr eam& , long_int& );
private:
base _radix;
string _str ;
int _len ;
long_int x_digit(const int&)const;
void x_radix();
void carry(VAF&)cons t;
string put_str()const;
static char itoc(const int& );
static int ctoi(const char&);
public:
long_int(base radix=RAD):_rad ix(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+=(cons t 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*=(cons t long_int&);
long_int& operator/=(const long_int&);
long_int& operator%=(cons t long_int&);
long_int operator!()cons t;
long_int operator-()const;
long_int operator+()cons t;
long_int sqrt()const;
long_int abs()const;
void radix_convert(b ase radix=RAD);
int size()const;
bool operator<(const long_int&)const ;
bool operator>(const long_int&)const ;
bool operator==(cons t long_int&)const ;
bool operator!=(cons t long_int&)const ;
bool operator<=(cons t long_int&)const ;
bool operator>=(cons t 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!=9332621544 394415268169923 885626670049071 596826438162146 859296389521759 9
993229915
608941463976156 518286253697920 827223758251185 210916864000000 000000000000000 0
00 (DEC) =
1B30964EC395DC2 4069528D54BBDA4 0D16E966EF9A70E B21B5B2943A321C DF10391745570CC A
9420
C6ECB3B72ED2EE8 B02EA2735C61A00 000000000000000 0000000 (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
1671
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++ while trying.) I realize this code is long but, I'm not sure of any other way to display the issue. The header file and ?implementation file work. (This assignment was building on another in which I created the header and implementation with a...
19
3345
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 something else? I ask because I want to use vector<bool> with a few billion entries exceeding the int32 range for indexing and I want to use as few memory as possible for the whole
2
2278
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 long count = 0;
1
1596
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 set, normalizing it to 0x00000001. If it wants to normalize the value then it should only operate on the al register since that's all that the native bool method uses to hold the return value. Is this a known VC 7.1 bug? Is there a hotfix...
9
7120
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 when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
8
3372
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" calls function "b" in file-2 which,
2
1625
by: Asim Qazi | last post by:
Hi All public class MyResponse { public bool m_bStatus; public string m_szErrorCode; public string m_szMessage; }
4
4353
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 Architecture. The Stored Procedures are used through TableAdaptors, which in turn are used by Class Files. I wish to be able to return this new ID value using the Stored
11
1979
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 return the string message ex.message, just wondering how to do this since I think you can only return one thing with the function. Thanks. public bool checkdate(String s_date) { b_convert = true ; DateTime dt_temp1 =...
0
7981
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8127
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8320
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6803
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3952
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2458
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1315
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.