?
Solved

inheritance of streams

Posted on 2004-11-04
13
Medium Priority
?
1,019 Views
Last Modified: 2013-12-14
This is more meant as a brain teaser question, I can easily get around the problem by copying a class and creating two completely separate classes but I'd like to know if anyone has a way to complete the inheritance properly.  Here it goes

I am trying to create some extra functions for the stl stream classes.  In the end I would like to have two classes, MyInputStringStream and MyInputFileStream.  Both classes use the same custom methods that I have created but one is inherited from stringstream and the other from fstream.

The iheritance would look something like this:

              __iostream___
            /           |          \
      fstream MyStream   stringstream
       \            /          \         /
  MyInputFileStream   MyInputStringStream


This is what I have tried writing as class definitions:

class MyStream : virtual public iostream {  /* funct defs here */ }

class MyInputFileStream : public MyStream , public fstream { };
class MyInputStringStream : public MyStream, public stringstream {};

The compilier is finding this ambiguous when making function calls

If i try to change iostream to ios I am unable to define my function new functions which use iostream calls.

I know that if I was not using predefined classes then MyStream would inherit iostream and then fstream and stringstream would each inherit MyStream but that doesn't seem to be an option this time.

Anyone that wants to have fun with inheritance feel free to play around with it.

Thanks,
  gkatz
0
Comment
Question by:gkatz
  • 8
  • 5
13 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12495780
Have you  tried making virtual  the multiple inheritance?

class MyStream : virtual public iostream {  /* funct defs here */ }

class MyInputFileStream : virtual public fstream, public MyStream{ };
class MyInputStringStream : virtual public stringstream, public MyStream{};

0
 
LVL 3

Author Comment

by:gkatz
ID: 12495897
sorry the compilier still sees it as being ambiguous
0
 
LVL 30

Expert Comment

by:Axter
ID: 12495971
I'm not getting a compile error.
Are you including all the required headers

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

class MyStream : virtual public iostream {  /* funct defs here */ };

class MyInputFileStream : virtual public fstream, public MyStream{ };
class MyInputStringStream : virtual public stringstream, public MyStream{};
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 30

Expert Comment

by:Axter
ID: 12496057
FYI:
I think you're better off using a template for this requirement.

Example:
template<class T>
class MyStream : public T {  /* funct defs here */ };

void TestCode()
{
      MyStream<stringstream> MyInputStringStream;
      MyStream<fstream> MyInputFileStream;
}

0
 
LVL 3

Author Comment

by:gkatz
ID: 12496064
it's not a compilier error, It's occuring when I try to use the MyInputFileStream class and use one of the iostream functions that applies to it.

in other words

int i;
MyInputFileStream f;
f  >> i;

f >> i causes an error because it is ambiguous.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12496079
If you want to add more detailed functionallity to each type you can also do the following:

template<class T>
class MyStream : public T {  /* funct defs here */ };

class MyInputStringStream : public MyStream<stringstream>{ };
class MyInputFileStream : public MyStream<fstream>{ };

void TestCode()
{
      MyInputStringStream myinputstringstream;
      MyInputFileStream myinputfilestream;
}

0
 
LVL 30

Expert Comment

by:Axter
ID: 12496088
>> f >> i causes an error because it is ambiguous

If you use the template method, that should remove the ambigouity.
0
 
LVL 3

Author Comment

by:gkatz
ID: 12496152
That could work, but here's an issue part of the advantage of using the inheritance was to allow functions to exist that coule use either type of stream.  If I use a template then I would need to overload the function rather than just allow the user to pass an object that has a base class of MyStream.

Thanks for the input.  I may try using a template.

gkatz

0
 
LVL 30

Expert Comment

by:Axter
ID: 12496228
>>If I use a template then I would need to overload the function rather than just allow the user to pass an
>>object that has a base class of MyStream.

I'm not sure what you mean by this.

Can you give an example code showing what you would gain via your original method?
0
 
LVL 3

Author Comment

by:gkatz
ID: 12497049
an example would be

//not using templates
//this function could accept both MyInputFileStream and  MyInputStringStream
someFunction(MyStream stream) {
   string str;
   stream >> str;
   cout  << str;
}

//using templates
//need two functions
someFunction(MyStream<fstream> stream ){
   string str;
   stream >> str;
   cout  << str;
}

someFunction(MyStream<stringstream> stream ){
   string str;
   stream >> str;
   cout  << str;
}



0
 
LVL 30

Expert Comment

by:Axter
ID: 12497221
For something like that, all you have to do is make your function take iostream type, and it will still work with templates.

Example:
template<class T>
class MyStream : public T {  /* funct defs here */ };

class MyInputStringStream : public MyStream<stringstream>{ };
class MyInputFileStream : public MyStream<fstream>{ };

void someFunction(iostream &stream) {
   string str;
   stream >> str;
   cout  << str;
}

void TestCode()
{
     MyInputStringStream myinputstringstream;
     MyInputFileStream myinputfilestream;
       someFunction(myinputstringstream);
       someFunction(myinputfilestream);
}
0
 
LVL 30

Accepted Solution

by:
Axter earned 300 total points
ID: 12497239
FYI:
If someFunction needs a specific functionallity of MyStream, then you can also make someFunction a template function.

Example:

template<class T>
class MyStream : public T
{
public:
      void foo(){}
};

class MyInputStringStream : public MyStream<stringstream>{ };
class MyInputFileStream : public MyStream<fstream>{ };

template<class T>
void someFunction(T &stream) {
   string str;
   stream.foo();
   stream >> str;
   cout  << str;
}

void TestCode()
{
     MyInputStringStream myinputstringstream;
     MyInputFileStream myinputfilestream;
       someFunction(myinputstringstream);
       someFunction(myinputfilestream);
}

0
 
LVL 3

Author Comment

by:gkatz
ID: 12497769
True a valid solution.

   Since this is supposed to be used by other programmers I think I will just stick with two separate classes just to make it more transparent to them.  Thanks for all of the ideas.  I was getting pretty annoyed that I could not find a solution.


gkatz

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question