Solved

Serialization of non-MFC

Posted on 1998-08-04
9
403 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now