Solved

How to Serialize a Vector Object

Posted on 2003-11-17
6
475 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sum28 challenge 31 95
canBalance challenge 34 65
Line meaning 9 76
mapBully challenge 6 88
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

16 Experts available now in Live!

Get 1:1 Help Now