Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Overloading the << and >> operators CArchive

Overloading the << and >> operators CArchive

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alton Williams
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • A Alton Williams

      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

      S Offline
      S Offline
      Steve S
      wrote on last edited by
      #2

      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 for myObj. So when you implement CArchive& operator<<( CArchive& ar, myObj obj) { // Output your member data here or whatever ... return ar; } and similarly for the operator>> function. Steve S

      A 1 Reply Last reply
      0
      • S 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 for myObj. So when you implement CArchive& operator<<( CArchive& ar, myObj obj) { // Output your member data here or whatever ... return ar; } and similarly for the operator>> function. Steve S

        A Offline
        A Offline
        Alton Williams
        wrote on last edited by
        #3

        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:

        1. place the functions in the appropiate C++ and header files
        2. subclass them from CObject (which I don't want to do)
        3. or find the correct syntax (which is):confused:

        Alton There's no problem, only solutions John Lennon

        S 1 Reply Last reply
        0
        • A Alton Williams

          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:

          1. place the functions in the appropiate C++ and header files
          2. subclass them from CObject (which I don't want to do)
          3. or find the correct syntax (which is):confused:

          Alton There's no problem, only solutions John Lennon

          S Offline
          S Offline
          Steve S
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups