473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

problem on saving object relations.

Dear All,

Assume I have two classes: material and shape, as follows

class Material
{
double density; // material attribute, may have more
vector<Shape*pShape; // shape objects assocaited the material.
};

class Shape
{
double size; // shape attribute, may have more
vector<Material*pMaterial; // material objects associated with the
shape
};

and I have two objects:

vector<Material*pMaterial;
vector<Shape*pShape;

And I want to save the two objects into a file and then open it by
using fstream. But here pointer is used to represent the association
relation. If I simply save the pointer address in to the file, when
opening there is no gurantee to have those materials and shapes at the
same address. Is there any good way to solve the problem?

Thanks,

Shuisheng

Oct 30 '06 #1
3 1608
shuisheng wrote:
Dear All,

Assume I have two classes: material and shape, as follows

class Material
{
double density; // material attribute, may have more
vector<Shape*pShape; // shape objects assocaited the material.
};

class Shape
{
double size; // shape attribute, may have more
vector<Material*pMaterial; // material objects associated with the
shape
};

and I have two objects:

vector<Material*pMaterial;
vector<Shape*pShape;

And I want to save the two objects into a file and then open it by
using fstream. But here pointer is used to represent the association
relation. If I simply save the pointer address in to the file, when
opening there is no gurantee to have those materials and shapes at the
same address. Is there any good way to solve the problem?
How about creating an instance_id for each object created. And when you save
them use this instead of the pointer. If you use a map you can look them up by.

#include <map>

class Shape;

class Material
{
static unsigned int next_instance;
unsigned int instance;
std::map<int, Shape*Shape_id;

Material() : instance(next_instance++) {};
};

class Shape
{
static unsigned int next_instance;
unsigned int instance;
std::map<int, Material*Material_id;

Shape() : instance(next_instance++) {};
};
Adrian
Oct 30 '06 #2
I think you can adjust the order of saving to keep this relationship.
For example, after you saving a material object, you save all shape
objects whose pMaterial equal the material object's address.
Adrian 写道:
shuisheng wrote:
Dear All,

Assume I have two classes: material and shape, as follows

class Material
{
double density; // material attribute, may have more
vector<Shape*pShape; // shape objects assocaited the material.
};

class Shape
{
double size; // shape attribute, may have more
vector<Material*pMaterial; // material objects associated with the
shape
};

and I have two objects:

vector<Material*pMaterial;
vector<Shape*pShape;

And I want to save the two objects into a file and then open it by
using fstream. But here pointer is used to represent the association
relation. If I simply save the pointer address in to the file, when
opening there is no gurantee to have those materials and shapes at the
same address. Is there any good way to solve the problem?

How about creating an instance_id for each object created. And when you save
them use this instead of the pointer. If you use a map you can look them up by.

#include <map>

class Shape;

class Material
{
static unsigned int next_instance;
unsigned int instance;
std::map<int, Shape*Shape_id;

Material() : instance(next_instance++) {};
};

class Shape
{
static unsigned int next_instance;
unsigned int instance;
std::map<int, Material*Material_id;

Shape() : instance(next_instance++) {};
};
Adrian
Oct 31 '06 #3

shuisheng wrote:
Dear All,

Assume I have two classes: material and shape, as follows

class Material
{
double density; // material attribute, may have more
vector<Shape*pShape; // shape objects assocaited the material.
};

class Shape
{
double size; // shape attribute, may have more
vector<Material*pMaterial; // material objects associated with the
shape
};

and I have two objects:

vector<Material*pMaterial;
vector<Shape*pShape;

And I want to save the two objects into a file and then open it by
using fstream. But here pointer is used to represent the association
relation. If I simply save the pointer address in to the file, when
opening there is no gurantee to have those materials and shapes at the
same address. Is there any good way to solve the problem?
There are several solutions to this. Look up information about object
streaming.

The basics is to hold a map of something like:

std::map< void *, int pointers;

As you serialise every time you come across a new pointer you enter it
in the map something like this:

pointers[ p ] = pointers.size();

You then write the number that you have in the map into the file
instead of the pointer.

As you load them from disk you do the reverse:

std::map< int, void * pointers;

You will need to use a placement new to construct the objects and
you'll also need to be able to get the types somehow, some sort of
factory for the objects you stream. There are a number of these sorts
of detail that you need to get straight and they can get quite tricky.

Many years ago (using C++ compilers much worse than those today) I
wrote a Dr. Dobbs article explaining the approach in a bit more detail.
I wouldn't implement it anything like that today with a decent compiler
though, although you should be able to pick up more detail on the
general approach.
http://www.ddj.com/184409645?pgno=3
K

Oct 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Keith | last post by:
I am having a problem creating a many-to-many-to-many type relationship. It works fine, but when I create a view to query it and test it, it does not generate the results I expected. Below...
14
by: MLH | last post by:
GHudson has working procedures posted at http://www.access-programmers.co.uk/forums/showthread.php?t=66320 They work. Relationships, however, and some minor default settings are not preserved....
0
by: Andrew Westgarth | last post by:
Hi all, i'm struggling with a page idea I have. I need to write a page with an A to Z list of available schools in the area. I only want to display the letters in the A to Z which have school...
8
by: I am Sam | last post by:
Hi everyone, This problem is making me old. I don't want to get any older. I have a multi-nested repeater control as follows: <asp:Repeater ID="clubRep1" Runat="server">...
5
by: Mika M | last post by:
Hello! I have Windows Forms application form containing TextBoxes, six ComboBoxes, and DataGrid for details. I have created DataSet with needed tables, and created relations between tables, and...
6
by: Brian Henry | last post by:
Here's an example of the code.. I have two combo boxes on screen that when one's selection is change the other's items will be updated to reflect the change (based on a relation) Private...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
28
by: ensemble | last post by:
I'm trying to utilized a more object-oriented approach to managing window events in javascript. Thus, I am creating a "controller" object to handle events and interact with the server. However, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.