Overloading the << and >> operators CArchive
-
I know to code it to return an object of ostream &, istream & or the classes that derived from them. But how do you do it to return a reference to a CArchive? Alton
-
I know to code it to return an object of ostream &, istream & or the classes that derived from them. But how do you do it to return a reference to a CArchive? Alton
If you're overloading the << and >> operators, chances are that you also have a CArchive& parameter. Simply return that. Usually you'll have something like
friend CArchive& operator<<( CArchive& ar, myObj obj); friend CArchive& operator>>( CArchive& ar, myObj& obj);
inside your class declaration formyObj
. So when you implementCArchive& operator<<( CArchive& ar, myObj obj) { // Output your member data here or whatever ... return ar; }
and similarly for theoperator>>
function. Steve S -
If you're overloading the << and >> operators, chances are that you also have a CArchive& parameter. Simply return that. Usually you'll have something like
friend CArchive& operator<<( CArchive& ar, myObj obj); friend CArchive& operator>>( CArchive& ar, myObj& obj);
inside your class declaration formyObj
. So when you implementCArchive& operator<<( CArchive& ar, myObj obj) { // Output your member data here or whatever ... return ar; }
and similarly for theoperator>>
function. Steve SSteve S wrote: friend CArchive& operator<<( CArchive& ar, myObj obj); friend CArchive& operator>>( CArchive& ar, myObj& obj); return ar; I've those that inside a generic underived class but the compiler complains i.e. what the hell am I talking about. do I:
- place the functions in the appropiate C++ and header files
- subclass them from CObject (which I don't want to do)
- or find the correct syntax (which is):confused:
Alton There's no problem, only solutions John Lennon
-
Steve S wrote: friend CArchive& operator<<( CArchive& ar, myObj obj); friend CArchive& operator>>( CArchive& ar, myObj& obj); return ar; I've those that inside a generic underived class but the compiler complains i.e. what the hell am I talking about. do I:
- place the functions in the appropiate C++ and header files
- subclass them from CObject (which I don't want to do)
- or find the correct syntax (which is):confused:
Alton There's no problem, only solutions John Lennon
Your C++ class must be derived from CObject to be serialisable anyway (see DECLARE_SERIAL, IMPLEMENT_SERIAL). You would put those definitions in your 'base' class's header file, with the implementation, which is normally to call obj.Serialize(CArchive&ar), which you then implement. You can then use these objects in your document's serialize method, using the operators, or alternatively, call the object's serialize method directly. Does that help any? Steve S