Solved

How to Serialize a Vector Object

Posted on 2003-11-17
6
517 Views
Last Modified: 2013-11-20
Hi,

I was looking  for an idea on how to implement saving a vector object using serialization.

The idea is it is custom class. I know that process on how to make a custom class seriable but I cannot get an idea on how to save a vector object.

I hope somenone can help on these.

Best regrards,
Mhelinx
0
Comment
Question by:mhelinx09
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9762250
A mathematical vector is a distance and a direction.  That involves saving just the two components.

What do you mean 'save a vector object' ?
To serialise a class just save the member variables you require permanence for.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9762256
If you mean save a list of objects created with new then save first the count of the obects, then each individual in turn.  To read you read in how many there are then implement the further reading in a loop.
0
 

Author Comment

by:mhelinx09
ID: 9762270
Hi Andy,

Thanks again for your help. This is the scenario:

I have a class:

class cSerial:

public:

int data1;
int  data2;

Then, I created a vector of this class:

vector<cSerial*>m_serial;

Now, I want to save it serially using WriteObject:

CArchive arStore(&myFile, CArchive::store)

arStore.WriteObject(&m_serial);

Is this possible?

Thanks in advance,
Mhelinx





0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9762402
I think you will need to use a loop and serialise each element individually.

something like

int vSize = m_serial.size();
arStore << (UINT)vSize;
for(int i = 0; i < vSize; i++)
  arStore.WriteObject(m_serial[i]);
0
 

Author Comment

by:mhelinx09
ID: 9769700
Hi Andy,

I know how to implement it individually. The thing is I'm comparing it with java. In java, you can use "writeObject(Vector CData)" . Then I know that there is WriteObject in C++ but I dont know if it can accept vector data in WriteObject function. You need to implement a CObject inherit class and make that class seriable but still the question is how to pass a Vector object in WriteObject function.

Do anyone here knows how to implement it?

Thanks again,
Mhelinx
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 100 total points
ID: 9769808
From the help files
CArchive::WriteObject
void WriteObject( const CObject* pOb );
throw( CFileException, CArchiveException );

Parameters

pOb

A constant pointer to the object being stored.

Remarks

Stores the specified CObject to the archive.

This function is normally called by the CArchive insertion (<<) operator overloaded for CObject. WriteObject, in turn, calls the Serialize function of the archived class.

You must use the IMPLEMENT_SERIAL macro to enable archiving. WriteObject writes the ASCII class name to the archive. This class name is validated later during the load process. A special encoding scheme prevents unnecessary duplication of the class name for multiple objects of the class. This scheme also prevents redundant storage of objects that are targets of more than one pointer.

The exact object encoding method (including the presence of the ASCII class name) is an implementation detail and could change in future versions of the library.

Note   Finish creating, deleting, and updating all your objects before you begin to archive them. Your archive will be corrupted if you mix archiving with object modification.



I think you would need to code your own WriteObject that iterates through the items in the vector (as I suggested earlier).  This standard WriteObject doesn't seem to have the possibility of a collection as a parameter
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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