Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualCornstalks

Posted 20 April 2013 - 03:06 PM

You could eliminate all the inheritance by simply having an internal/private helper class that is responsible for maintaining the current seek pointer, and has methods that read/write/seek/etc....

 

Then have publicly visible wrapper classes (OutputStream, RewindableStream, RewindableInputStream, etc....) that only implement the specific functionality you need. Internally they just delegate to the helper class which has all the functionality.

Essentially like what I had in C, but with a wrapper around it. The problem that I see now is that I can't pass an InputOutputStream as an OutputStream; since they aren't derived from each other, I would need some sort of unwieldy mechanism to convert between the two, rather than just passing it. I'm not trying to eliminate all inheritance, just to make my goals possible without making a monster that I won't want to use.

Out of curiosity, could templates work? It's an option to consider, at least.

 

If you wanted to maintain a design similar to C++'s standard library streams, you could have an inheritance diagram like:

Ah, so multiple-inheritance would be the best tool for this job, while maintaining the ability to cast the pointer of the derived to a pointer of the base, and not having to handle child objects that require workarounds and more complex syntax? If I don't have to use dynamic_cast, and keep the vtable size down, will this become problematic for any reason?

It should work. You won't have to dynamic cast things either. This is what C++'s stream classes do (basic_istream and basic_ostream virtually inherit from basic_ios, and basic_iostream inherits from both basic_istream and basic_ostream). You can pass a basic_iostream to a function that takes a basic_ostream by reference without any casting.


#1Cornstalks

Posted 20 April 2013 - 01:46 PM

You could eliminate all the inheritance by simply having an internal/private helper class that is responsible for maintaining the current seek pointer, and has methods that read/write/seek/etc....

 

Then have publicly visible wrapper classes (OutputStream, RewindableStream, RewindableInputStream, etc....) that only implement the specific functionality you need. Internally they just delegate to the helper class which has all the functionality.

Essentially like what I had in C, but with a wrapper around it. The problem that I see now is that I can't pass an InputOutputStream as an OutputStream; since they aren't derived from each other, I would need some sort of unwieldy mechanism to convert between the two, rather than just passing it. I'm not trying to eliminate all inheritance, just to make my goals possible without making a monster that I won't want to use.

Out of curiosity, could templates work? It's an option to consider, at least.

 

If you wanted to maintain a design similar to C++'s standard library streams, you could have an inheritance diagram like:

Ah, so multiple-inheritance would be the best tool for this job, while maintaining the ability to cast the pointer of the derived to a pointer of the base, and not having to handle child objects that require workarounds and more complex syntax? If I don't have to use dynamic_cast, and keep the vtable size down, will this become problematic for any reason?

It should work. You won't have to dynamic cast things either. This is what C++'s stream classes do (basic_istream and basic_ostream virtually inherit from basic_stream, and basic_iostream inherits from both basic_istream and basic_ostream). You can pass a basic_iostream to a function that takes a basic_ostream by reference without any casting.


PARTNERS