• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Serialization of non-MFC

In MS Scibble example one has the following code:
void CScribbleDoc::Serialize(CArchive& ar)
{
      if (ar.IsStoring())
            ar << m_sizeDoc;
      else
            ar >> m_sizeDoc;
      m_strokeList.Serialize(ar);
}
m_strokeList is a MFC. It has a serialze function.
What does one do if your want to serialize a non-MFC.
Current
"MyClass.h"
#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass
{
private:
      int m_maj;
      float m_min;
public:
      MyClass();
};
#endif
"MyClass.cpp"
#include "MyClass.h"

MyClass::MyClass()
{
      m_maj = 3;
      m_min = 3.3;
}
"Serialize_MyClass.h"
#ifndef SERIALIZE_MYCLASS_H
#define SERIALIZE_MYCLASS_H

class CArchive;
class MyClass;

CArchive &operator << (CArchive &ar, const MyClass *pMyClass);

#endif
"Serialize_MyClass.cpp"
#include <stdafx.h>
#include "Serialize_MyClass.h"
#include "MyClass.h"
CArchive &operator <<(CArchive& ar, const MyClass *pMyClass)
{
...
            return ar;
}
I then include Serialize_MyClass into ScribbleDoc.cpp file.
I try ar << m_MyClass; //member variable define as MyClass
I get the following error:
error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class CArchive' (or there is no acceptable conversion)
NO CODE is to be added to MyClass. Please show header files and cpp files and code. I believe I have to override operator << for CArchive to handle class MyClass. In the overridden function I have to ar << (simply data types).
Is this correct at all? Please also show if I am using classes by name only, what include files I need.
Help
0
Paullkha
Asked:
Paullkha
1 Solution
 
PaullkhaAuthor Commented:
Edited text of question
0
 
PaullkhaAuthor Commented:
Edited text of question
0
 
PaullkhaAuthor Commented:
Adjusted points to 80
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
t004024Commented:
Look at the article in MFC help :

CObject Class topics,  in Adding Program Functionalities, Details

As far as I understood U need to have the class derived from CObject, only then U can add the serialization functionality and U don't seem to have done that.
0
 
PaullkhaAuthor Commented:
Mike Blaszczak, author of Professional MFC VC++5, states that there are 3 ways one can use for types not supported by CArchive, overload <<, develope a macro, or read or write the type. He provides no examples however.
0
 
ArkadiyCommented:
You say: "member variable define as MyClass". However, the operator you define
CArchive &operator << (CArchive &ar, const MyClass *pMyClass);
expects a pointer to MyClass. You have to change the operator header to
CArchive &operator << (CArchive &ar, const MyClass &pMyClass);

0
 
glasgowCommented:

You can serialize any class using CArchive and the << operator.

When MFC serializes objects, it writes out the class of the
object it is serializing first, then the version of the object,
then it calls the objects serilize routine to actually write the
data. When it reads an object in, the class of the object is
read, and an object of this type is created.  the version is
read, and then the serialize routine of the object is called to
load the data into the object.

For any object to use CArchive, you need to set up the runtime
information for your class.  There are a  bunch of macros in
MFC to do this.  The ones that you need are DECLARE_SERIAL
and IMPLEMENT_SERIAL.   Your class would look like:

class CMyClass : public CObject {

  DECLARE_SERIAL(CMyClass)

public:
 ... rest of class
};


in cpp file:

//  class, parent_class, and version number of your object.
IMPELEMENT_SERIAL(CMyClass, CObject, 0x100);

void
CMyClass::Serialize( CArchive  &ar )
{
}


These macros will set up the runtime class information, and
overload all of the << and >> operators so that you can
serialize your objects just like any other MFC class.
This class will now work with the MFC colleciton classes.
So, if you have a CObArray of CMyClass structues, you can
serialize the entire class.

Michael


0
 
PaullkhaAuthor Commented:
Arkadiy.
THANKS! There was a bug in the code, but why did it keep saying left hand side?. I actually did I quick proto to see if I could overload the operator<< the way I was. I did not make that mistake in the quick proto. Oh Well.
However, is this the proper way to accomplish what I want?
ar << m_MyClass will cause the overloaded function to perform the following tasks:
ar << simpletype1_in_MyClass
ar << simpletype2_in_MyClass
Also, should I make the overloaded function a friend of MyClass, accessing the private members directly? Or call functions to get the private data members? I believe the latter is more secure since overloaded operator<< and operator>> are in a seperate module.
0
 
ArkadiyCommented:
Well, a better error message would be something like "There is no operator<<(CArchive&, MyClass&)". I can't tell why the compiler's author did it that way, but there probably is a reason.

Conserning the things you do in your operator... That's up to you, isn't it? I see three options: you can just call Write/Read on the entire instance (works if you have no virtual functions, pointers, subclasses etc.), or you can do what you do now.

Calling functions to get private members is fine as long as you have functions to set them as well :). And if the functions are just asignement operators, consider if you really need them.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now