问题
I was just wondering why the member functions tellg()
defined in basic_istream<>
class and tellp()
defined in basic_ostream<>
class have different names. Is that because basic_fstream<>
is derived from basic_istream<>
and basic_ostream<>
?
回答1:
And how would you distinguish them in bidirectional streams,
like std::fstream
or std::stringstream
? Streams are allowed
to maintain separate pointers for input and output: fstream
doesn't, but stringstream
does. So you need either a flag to
indicate which one you want (as is the case in streambuf
), or
you need two separate functions.
回答2:
tellg()
get the position of the get
pointer and tellp()
gets the position of the put
pointer, one of them is the place where you read and the second- where you write in the file. So the two functions do different things and return different values. Why would you think they should have the same name?
来源:https://stackoverflow.com/questions/14522122/difference-between-basic-istreamtellg-and-basic-ostreamtellp