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
Solved

How to Serialize a Vector Object

Posted on 2003-11-17
6
503 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 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