[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to Serialize a Vector Object

Posted on 2003-11-17
6
Medium Priority
?
530 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 45

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 45

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 45

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 45

Accepted Solution

by:
AndyAinscow earned 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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 lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

834 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