-4

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);
}
Akib Azmain Turja
  • 1,142
  • 7
  • 27
  • 2
    Don't use owning bare pointers. Currently, your class leaks memory. – eerorika Jul 30 '20 at 14:43
  • Describe in more detail "can't handle" and "misbehaves". How does it behave? What did you expected instead? – eerorika Jul 30 '20 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'. – john Jul 30 '20 at 14:52
  • To confirm john's statement here's a link with a good write up https://artofcode.wordpress.com/2010/12/12/deriving-from-stdstreambuf/ – Francis Cugler Jul 30 '20 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` . – Akib Azmain Turja Jul 30 '20 at 15:37
  • @eerorika I mean that sometime my program doesn't compile, sometime segmentation fault. – Akib Azmain Turja Jul 30 '20 at 15:40
  • @AkibAzmain `I mean that sometime my program doesn't compile` Are you saying that without making changes to the program, or the compiler, it arbitrarily might compile or not? That's quite unusual. – eerorika Jul 30 '20 at 15:43
  • @eerorika I'm trying to make a library, the test suite was compiled before I modified and compiled my library, so when I run test suite before recompiling, it shows segmentation fault, when trying to recompile, compilation error. – Akib Azmain Turja Jul 30 '20 at 17:17
  • @AkibAzmain Sounds like the segfault is due to incompatibility of the test build with the modified library. Did the compiler not produce an error message? That's where you should start fixing the problem. – eerorika Jul 30 '20 at 17:28
  • @AkibAzmain Then why are you deriving from iostream? – john Jul 31 '20 at 06:23
  • @john streambuf provides buffer to handle io, not a friendly interface to do io operation. – Akib Azmain Turja Jul 31 '20 at 15:23
  • @AkibAzmain What I meant was why are you deriving from iostream at all. What's the benefit? It seems like your mystream class has no relation to iostream. But just my opinion based on what you've posted here, I'm sure you know what you're doing. – john Jul 31 '20 at 17:46
  • @john OK, let me explain. Previously I had one class for stream, myclass. After defining it I found that the functions conflicts with the base class (I haven't specified those function names in the question). So now, I defined another function `io()` and class `mystream` to hold anything related with stream. Function `io` will return the stream, so any stream operations must be done by going through `io` . It works perfectly, but I want to use `<<` and `>>` operators directly by object not by `io` and any other function by `io` . – Akib Azmain Turja Aug 01 '20 at 04:39

1 Answers1

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();
Akib Azmain Turja
  • 1,142
  • 7
  • 27