-5

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);
}
13
  • 2
    Don't use owning bare pointers. Currently, your class leaks memory. Commented Jul 30, 2020 at 14:43
  • Describe in more detail "can't handle" and "misbehaves". How does it behave? What did you expected instead? Commented Jul 30, 2020 at 14:48
  • The normal way to make a class with stream functionality is to write a class derived from 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'. Commented Jul 30, 2020 at 14:52
  • To confirm john's statement here's a link with a good write up artofcode.wordpress.com/2010/12/12/deriving-from-stdstreambuf Commented Jul 30, 2020 at 14:56
  • @john @francis I know how to define class with stream functionality. But my problem is many function in my class conflicts with functions defined in basic_iostream . Commented Jul 30, 2020 at 15:37

1 Answer 1

2

Calling functions from parent class directly is easy as the following:

std::stringstream myobj;
myobj << "Something..."
myobj.std::iostream::flush();

Above example calls flush() directly from std::iostream (which is a typedef of std::basic_iostream<char> ). It is also possible to member functions from parent class of parent class:

myobj.std::ostream::flush();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.