Solved

How to Serialize a Vector Object

Posted on 2003-11-17
6
495 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running shell command from Access 3 72
ASCII Non-Printable characters/codes and their HTML equivalents 6 114
Line meaning 9 85
How to convert MFC::CString to UTF8 wchar_t* 10 201
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

806 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