> short int size;
1. Why short? <<--- I know the size will not be greator than 256
2a. Why are you allocating one additional byte? <<--- I think I should
have initialilzed the data member "temp" to NULL.
2b. Why is size zero. <<-- Just initializing this to a value
"You also have a rule of three violation. Your object will blow
up if copied or assigned. You need to do a copy constructor and
a copy assignment operator. Again this wouldn't be a problem if
temp were a vector or string. These classes already have these
defined."
Yeap, can't do without the copy and assignment constructor.
3. Yeap! delete [] temp; <--- missed this one.
"This is NOT the way to check for end of file. The eof state is
only set AFTER a read fails.
while(infile >> object_abc) {
will do what you want (it will fail when there is an error or eof, you
can then check the eof flag if you need to differentiate). "
.... hmm ... I have to make a note of this one.
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++
You haven't told us how you know what the data is that you are reading?
What is the nature of the file?
You can't use a >> char* without knowing some upper bound on the length.
This isn't a limitation with the string class though.
I could have used string type for the data members.
However, I didn't want the overhead that comes with the string class.
A sample data file, would be as follows
xyz.txt
some_name1 value1
some_name2 value2
..
..
Having said that my class definition needs two different data members,
i didn't include that in my previous post.
class abc {
....
char * temp;
char * temp2;
....
};
Other functions will definitely require amendments, and will
incorporate the second data member.
istream& operator >> (istream & is, abc &obj)
{
is >> obj.temp >> obj.temp2;
return is;
}
I really do appreciate your suggestion, besides using another class
string or stringstream, if I stick with "char *"
I am still not sure how, when, and where to allocate memory for "temp
and temp2" data members.
Regards,
Manish