overload << for a base class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

overload << for a base class

 
0
  #1
Nov 6th, 2008
I have a base class called ModelFile

I have derived classes called ObjFile and VtkFile that I would like both to use << from ModelFile.

However, since << is an external function,
  1. ostream & operator << (ostream &output, const ModelFile &Model)
  2. {
  3. output << "Num Vertices: " << Model.NumVertices() << endl
  4. << "Num Triangles: " << Model.NumTriangles() << endl;
  5. return output;
  6. }

The compiler doesn't know about
  1. ostream & operator << (ostream &output, const ObjFile &Obj)

Is there a way to do this?

Thanks,
Dave
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: overload << for a base class

 
0
  #2
Nov 6th, 2008
To my knowledge, the only way to use the same version of << for both classes would be if they output exactly the same information about both classes. In that case you might be able to put the << operator in a base class and then let both classes inherit the same << operator from the base class.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: overload << for a base class

 
0
  #3
Nov 6th, 2008
yea they do output the same information. So how do I make them inherit the operator?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: overload << for a base class

 
0
  #4
Nov 6th, 2008
struct base
int data;
void printData() {cout << data;}

struct V : public base
string name;
void printName() {cout << name;}

struct C : public base
char gender;
void showGender() {cout << gender;}

V v;
v.name = "me";
v.data = 1;

C c;
c.gender = 'F';
c.data = -9999;

v.printData();
c.printData();

Each V object and each C object inherit an int member from base called data and a function from base to display that value. However, no base object contains either a name or a gender or a method to display that information. The printData() method cannot be modified in any derived class for it's own specifications unless you were to declare it with the keyword virtual. Even then, the function declaration in the derived classes needs to be the same, although the implementation can be different.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: overload << for a base class

 
0
  #5
Nov 7th, 2008
right, but the problem is that the << operator is not a member function, so it is not inherited, right?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: overload << for a base class

 
0
  #6
Nov 7th, 2008
  1. struct base
  2. {
  3. virtual std::ostream& write( std::ostream& stm ) const = 0 ;
  4. // ...
  5. };
  6.  
  7. inline std::ostream& operator<< ( std::ostream& stm, const base& object )
  8. {
  9. return object.write( stm ) ; // polymorphic
  10. }
  11.  
  12. // ----------------------------------------------------------------------------------------------------
  13.  
  14. struct derived : base
  15. {
  16. // override
  17. virtual std::ostream& write( std::ostream& stm ) const
  18. {
  19. // write object out to stream
  20. return stm ;
  21. }
  22. // ...
  23. };
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: overload << for a base class

 
0
  #7
Nov 8th, 2008
So that is a write() function, can you not do that with the actual << operator?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: overload << for a base class

 
0
  #8
Nov 8th, 2008
Copy, paste, compile and run the following program and see if it meets your criteria of using the same << for different classes derived from a common base class. The program is obviously based on the code snippet from vijayan21, whom I applaud for the nifty snippet!

Hopefully this works not just because of some quirk in my compiler (VC++ 6), though I respect vijayan21 enough to expect it to work with any reasonable compiler.
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct base
  6. {
  7. virtual std::ostream& write( std::ostream& stm ) const = 0;
  8. // ...
  9. };
  10.  
  11. inline std::ostream& operator<< ( std::ostream& stm, const base& object )
  12. {
  13. return object.write( stm ) ; // polymorphic
  14. }
  15.  
  16. // ----------------------------------------------------------------------------------------------------
  17. struct derived : base
  18. {
  19. // override
  20. virtual std::ostream& write( std::ostream& stm ) const
  21. {
  22. // write object out to stream
  23. stm << data << endl;
  24. return stm ;
  25. }
  26.  
  27. int data;
  28. };
  29.  
  30. //..................................................
  31. struct anotherDerived : base
  32. {
  33. // override
  34. virtual std::ostream& write( std::ostream& stm ) const
  35. {
  36. // write object out to stream
  37. stm << data << " and " << i << endl;
  38. return stm ;
  39. }
  40.  
  41. double data;
  42. int i;
  43. };
  44.  
  45. int main()
  46. {
  47. derived d;
  48. d.data = 44;
  49. cout << d << endl;
  50.  
  51. anotherDerived ad;
  52. ad.data = 9.9999;
  53. ad.i = -687;
  54. cout << ad << endl;
  55.  
  56. return 0;
  57. }
Note there's only a single declaration and definition of the << operator which works for all classes derived from the base class for which it was written. To me it fits the following scenario as posted in your first post, but you can be the final judge.

>>I have a base class called ModelFile. I have derived classes called ObjFile and VtkFile that I would like both to use << from ModelFile.
Last edited by Lerner; Nov 8th, 2008 at 9:16 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC