LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2004, 06:00 PM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
c++ overloading input operator


i need to make a HugeInt class that can have up to 30 integers in it, and it can not be negative, i dont know how to overload the input operator... i am thinking i need to know how long the string/number is that is entered...

Code:
istream &operator>> (istream &inStream, HugeInt &obj){
     //need a loop somehow to set each digit to integer[i]
     inStream >> setw(1) >> obj.integer[i];
}
there is a private array of integers which represents each digit of the number, where

HugeInt(1234);

integer[0] = 1
integer[1] = 2
integer[2] = 3
etc.
 
Old 02-24-2004, 06:07 PM   #2
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
figured it out...

istream & operator>> ( istream &inStream, HugeInt &obj)
{
char *temp;
inStream >> setw(30) >> temp;

obj = temp;

return inStream;
}
 
Old 02-24-2004, 08:51 PM   #3
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Another, possibly more robust way, is to use a loop to read in characters until something that isn't a digit is found.

Also, I think you may want to use char temp[30] instead of char *temp, since just using a pointer does not actually allocate any space for characters. I guess the istream >> operator might allocate it, but you can never be too sure
 
Old 02-25-2004, 06:09 PM   #4
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
in the overloaded = operator, it handles that, so *temp is ok...
 
Old 02-25-2004, 07:24 PM   #5
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Actually I was referring to the part where you read stuff in to *temp, before the "obj = temp" statement. Declaring "char *temp" only gives you a pointer; it doesn't actually give you any space to use. I just ran a test program with it, and it looks like it works, but it doesn't seem like the safest way to do it.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with overloading operator+ in C++ niteshadw Programming 13 07-20-2005 07:45 PM
Operator Overloading problem ashwinipahuja Programming 1 06-23-2004 05:59 AM
c++ [] operator overloading exodist Programming 12 04-17-2004 03:06 PM
C# with dotGNU, operator overloading exodist Programming 2 03-15-2004 05:52 PM
Builtin operator overloading. exodist Programming 15 03-11-2004 10:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration