iostream

hey guys just a few quick questions in I/O,I'm going over it at the moment

1) how come we iobase and ostream classes constructor protected basically not allowing us to create an instance of them?

1
2
3

ios_base i; // error
ostream o; // error 


2) I can't follow the inheritance of this http://www.cplusplus.com/reference/fstream/fstream/

does fstream inherit from ifstream or ofstream or both OR is it the other way around do ifstream and ofstream inherit from fstream?

3) when I include the fstream header file I can use both ofstream and ifstream types but when I include the ostream and ifstream header I file can't use either ifstream or ostream it pretty much tells me it can't find the ifstream or ofstream header file



thanks

adam2016 wrote:
1
2
ios_base i; // error
ostream o; // error 

An std::ios_base object would be useless because its members describe a stream that isn't there. As cppreference puts it at http://en.cppreference.com/w/cpp/io/ios_base/ios_base
cppreference wrote:
The default constructor is protected: only derived classes may construct std::ios_base.


As for std::ostream, its constructor is public, you can make ostreams just fine:

1
2
3
4
5
6
#include <iostream>
int main()
{
    std::ostream myout(std::cout.rdbuf());
    myout << "hello, world\n";
}


adam2016 wrote:
does fstream inherit from ifstream or ofstream or both

both. See also http://en.cppreference.com/w/cpp/io/basic_fstream

adam2016 wrote:
when I include the fstream header file I can use both ofstream and ifstream types but when I include the ostream and ifstream header I file can't use either ifstream or ostream

the types std::ifstream and std::ofstream are defined in the header <fstream>. The ostream header has nothing to do with file streams. There is no "ifstream header" in C++.
Last edited on
how come we iobase and ostream classes constructor protected basically not allowing us to create an instance of them?


Because the people who designed them decided that you shouldn't be allowed to create an instance of them, because they aren't useful to you. They don't do anything. They're meant to be used to build useful classes, via inheritance.
Topic archived. No new replies allowed.