| | |
overload << for a base class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
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,
The compiler doesn't know about
Is there a way to do this?
Thanks,
Dave
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)
ostream & operator << (ostream &output, const ModelFile &Model) { output << "Num Vertices: " << Model.NumVertices() << endl << "Num Triangles: " << Model.NumTriangles() << endl; return output; }
The compiler doesn't know about
C++ Syntax (Toggle Plain Text)
ostream & operator << (ostream &output, const ObjFile &Obj)
Is there a way to do this?
Thanks,
Dave
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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.
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
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
C++ Syntax (Toggle Plain Text)
struct base { virtual std::ostream& write( std::ostream& stm ) const = 0 ; // ... }; inline std::ostream& operator<< ( std::ostream& stm, const base& object ) { return object.write( stm ) ; // polymorphic } // ---------------------------------------------------------------------------------------------------- struct derived : base { // override virtual std::ostream& write( std::ostream& stm ) const { // write object out to stream return stm ; } // ... };
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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. 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.
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)
#include <iostream> using namespace std; struct base { virtual std::ostream& write( std::ostream& stm ) const = 0; // ... }; inline std::ostream& operator<< ( std::ostream& stm, const base& object ) { return object.write( stm ) ; // polymorphic } // ---------------------------------------------------------------------------------------------------- struct derived : base { // override virtual std::ostream& write( std::ostream& stm ) const { // write object out to stream stm << data << endl; return stm ; } int data; }; //.................................................. struct anotherDerived : base { // override virtual std::ostream& write( std::ostream& stm ) const { // write object out to stream stm << data << " and " << i << endl; return stm ; } double data; int i; }; int main() { derived d; d.data = 44; cout << d << endl; anotherDerived ad; ad.data = 9.9999; ad.i = -687; cout << ad << endl; return 0; }
>>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
![]() |
Similar Threads
- StreamReader and Position (C#)
- dynamic array help (C++)
- Write to Screen and File{I-O question} (C++)
- Virtual functions (C++)
- C++ Assist (C++)
- HELP: class static function - compile errors (C++)
Other Threads in the C++ Forum
- Previous Thread: please help me...
- Next Thread: How to deserialize binary stream
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






