M
marora
I have created class definition which contains
a charater pointer as one of it's data memeber.
The objective is to read some data from a file,
and assign it to a data member;
Size of data is not known in the begining.
We can assume that it will not exceed 256;
class definition:
class abc {
private:
char * temp;
short int size;
public:
abc() {
size = 0;
temp = new char [size + 1];
};
~abc() {
delete temp;
};
friend istream& operator >>(istream &is, abc & obj);
char * Get_temp() {
return temp;
};
};
istream& operator >> (istream & is, abc &obj)
{
is >> obj.temp;
return is;
}
int main ()
{
abc object_abc;
ifstream infile("xyz.txt");
assert(infile);
assert(infile.is_open());
while (!infile.eof()) {
infile >> object_abc;
cout << object_abc.Get_temp() << endl;
}
infile.close();
return 0;
}
My question is how, when, and where to allocate memory for the data
member "temp".
a charater pointer as one of it's data memeber.
The objective is to read some data from a file,
and assign it to a data member;
Size of data is not known in the begining.
We can assume that it will not exceed 256;
class definition:
class abc {
private:
char * temp;
short int size;
public:
abc() {
size = 0;
temp = new char [size + 1];
};
~abc() {
delete temp;
};
friend istream& operator >>(istream &is, abc & obj);
char * Get_temp() {
return temp;
};
};
istream& operator >> (istream & is, abc &obj)
{
is >> obj.temp;
return is;
}
int main ()
{
abc object_abc;
ifstream infile("xyz.txt");
assert(infile);
assert(infile.is_open());
while (!infile.eof()) {
infile >> object_abc;
cout << object_abc.Get_temp() << endl;
}
infile.close();
return 0;
}
My question is how, when, and where to allocate memory for the data
member "temp".