Solved

Serialization of non-MFC

Posted on 1998-08-04
9
405 Views
Last Modified: 2013-11-19
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
Comment
Question by:Paullkha
9 Comments
 
LVL 2

Author Comment

by:Paullkha
ID: 1320238
Edited text of question
0
 
LVL 2

Author Comment

by:Paullkha
ID: 1320239
Edited text of question
0
 
LVL 2

Author Comment

by:Paullkha
ID: 1320240
Adjusted points to 80
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Expert Comment

by:t004024
ID: 1320241
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
 
LVL 2

Author Comment

by:Paullkha
ID: 1320242
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
 
LVL 1

Accepted Solution

by:
Arkadiy earned 80 total points
ID: 1320243
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
 

Expert Comment

by:glasgow
ID: 1320244

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
 
LVL 2

Author Comment

by:Paullkha
ID: 1320245
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
 
LVL 1

Expert Comment

by:Arkadiy
ID: 1320246
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 128
mapBully challenge 6 152
firstChar challenge 13 121
how to split multiple lines delimiter : 8 111
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question