943,772 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1265
  • C++ RSS
Nov 6th, 2008
0

overload << for a base class

Expand Post »
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,
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. ostream & operator << (ostream &output, const ObjFile &Obj)

Is there a way to do this?

Thanks,
Dave
Similar Threads
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 6th, 2008
0

Re: overload << for a base class

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 6th, 2008
0

Re: overload << for a base class

yea they do output the same information. So how do I make them inherit the operator?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 6th, 2008
0

Re: overload << for a base class

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 7th, 2008
0

Re: overload << for a base class

right, but the problem is that the << operator is not a member function, so it is not inherited, right?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 7th, 2008
0

Re: overload << for a base class

C++ Syntax (Toggle Plain Text)
  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. };
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 8th, 2008
0

Re: overload << for a base class

So that is a write() function, can you not do that with the actual << operator?
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Nov 8th, 2008
0

Re: overload << for a base class

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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: please help me...
Next Thread in C++ Forum Timeline: How to deserialize binary stream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC