passing istream reference to derived funtion

Are there any restrictions on passing an istream reference to a derived function? Is there any way to make this work?

I've written a class with a derived subclass, both of which are controlled by a pointer management/handle class. I've also written a custom read function to read in my specially formatted data and determine the appropriate class type, depending on this data. When I use the function, everything works as expected until I pass into the derived read function. At this point, I successfully enter the appropriate function for the derived class. However, in this function I encounter a problem when I pass the istream reference to getline, which returns an empty line. The problem is that this istream should not be empty. When in the base class function (when appropriate) and while in the handle class function this stream is not empty and getline returns the actual line.

What am I doing wrong? Below is an a copy of the code's read2 functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
istream& handle::read2(istream& is)
{
	// delete previous object, if any
	delete tp;

	// read in line of file
	string data_line;
	getline(is,data_line);

	// put line back into file
	is.putback('\n');
	for(string::size_type i = 0; i < data_line.size(); i++) {
		is.putback(data_line[data_line.size()-i-1]);
		cout << data_line[data_line.size()-i-1];
	}
	cout << endl;

	// convert line to words
	vector<string> line_words = sentence(data_line);

	// if message, create new derived object, else create base
	if(line_words[18] != "empty") {
		tp = new derivedClass;
	} else {
		tp = new baseClass;
	}

	// read message for appropriate class type
	(*tp).read2(is);

	return is;
}

istream& baseClass::read2(std::istream& in)
{
	cout << "inside base read" << endl;

	string line;
	getline(in,line);
        cout << line << endl;

	return in;
}

istream& derivedClass::read2(std::istream& in)
{
	cout << "inside derived read" << endl;

	// read in first line of file
	string line;
	getline(in,line);
	cout << line << endl;
        return in;
}


Note that on execution baseClass::read2 outputs "inside base read" and a valid line, while derivedClass ::read2 outputs "inside derived read" but no valid line.
More information:

The problem seems to actually occur during the call to getline in derivedClass::read2. When I pass into read2, my input stream appears valid. However the call to getline destroys this stream. What gives?
Topic archived. No new replies allowed.