TL;DR
I have a class in which some function names conflict with the parent class's function. How to call functions from the parent class in C++?
My problem
I'm trying to make a class with stream functionality. So, I inherited my class from std::basic_iostream . But my class has many functions which conflict with the base class. So, I defined a dedicated class for stream and a function within the main class which will return the stream object. I implemented the constructor of the main class such way so it will create stream objects on object creation. My code was like this:
class mystream : public std::basic_iostream<char> {
...
};
class myclass {
protected:
mystream* stream;
public:
myclass() {
stream = new mystream(args);
}
mystream& io() {
return (*stream);
}
};
What I'm trying to do now
It works great when I access the stream with io member, but I want to use << and >> operator without calling io member and any member function of mystream with io member. Now I have to access stream like this:
myclass object;
object.io() << "Some output";
object.io().flush();
But I want to access << and >> operators directly from object and any member of mystream by io function, like this:
myclass object;
object << "Some output";
object.io().flush();
What I tried
I tried to add this to my class, but it misbehaves and can't handle IO manipulators like std::endl :
template <typename type>
std::ostream& operator << (type data) {
return ((*stream) << data);
}
template <typename type>
std::istream& operator >> (type &data) {
return ((*stream) >> data);
}
std::streambuf. All the iostream classes are is a wrapper around different streambuf derived classes. If you take that approach you'll get all the iostream functionality 'for free'.basic_iostream.