By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

Saving an object in file after having modified it using pointer

venere
P: 3
Hi everybody,

Well, suppose an inheritance relation between Show, Movie and Play, with Show being an abstract class. I save (using append) Movie and Play objects in two different binary files.

Wanting to edit both kinds of objects using dynamic binding, I create an array of Show pointers:
Show *show_arr[50];

and I make them pointing to my objects like this:

Movie m_obj;
file.open( "Movies" ,ios::binary|ios::in);
for ( j=0; j<movies_records_no; j++)
{
file.read((char *) & m_obj,sizeof( m_obj ));
show_arr[j] = new Movie( m_obj );
}
file.close();

Play p_obj;
file.open( "Plays" ,ios::binary|ios::in);
for (k=0; k<plays_records_no; k++)
{
file.read((char *) & p_obj ,sizeof( p_obj ));
show_arr[k+j] = new Play( p_obj );
}
file.close();

After having modified their private members I save them like this:

file.open("Movies",ios::trunc|ios::binary|ios::out );
for (i=0; i<movies_records_no; i++)
{
file.write((char *) show_arr[i], sizeof( m_obj ));
}
file.close();

and doing the same for Plays
( for (i=movies_records_no; i<movies_records_no + plays_records_no; i++) )

Where am I wrong? Please help me!!!

Thank you in advance,
Athena Alexopoulos.
Jul 30 '07 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
What do your class definitions look like?

You may not be writing what you think.
Jul 30 '07 #2

venere
P: 3
class Show
{
public:
// pure virtual functions e.g.
virtual void set_showData() = 0;

// inherited functions e.g.
char* getTitle() { return title; }

protected:
char title[30];
};

class Movie
{
};
Jul 30 '07 #3

venere
P: 3
class Show
{
public:
// pure virtual functions e.g.
virtual void set_showData() = 0;

// inherited functions e.g.
char* getTitle() { return title; }

protected:
char title[30];
};

class Movie: public Show
{
public:
void set_showData();
char* getGenre() { return genre; }

protected:
char genre[20];
};

class Play: public Show
{
public:
void set_showData();
char* getWriter() { return writer; }

protected:
char writer[30];
};

Actually it's a bit more complicated, but is it a "valid way" to write the pointer in that way; ( (char*) show_arr[i] )?

Thanks!
Jul 30 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
but is it a "valid way" to write the pointer in that way; ( (char*) show_arr[i] )?
No. show_arr is an array of Show* not char*.

Do not cast in C++. When you do it usually means your design is screwed up.

Now, you have a Show class:
Show
{
public:
// pure virtual functions e.g.
virtual void set_showData() = 0;

// inherited functions e.g.
char* getTitle() { return title; }

protected:
char title[30];
};
This tells me that you are going to use polymorphism and write object-oriented programs. In these programs, all functions must use pointers or references to the Show class.

Unfortunately, the set_showData() has no argument so there is no way tio change the Show title, which, by the way, should be private. You do not want a derived classed messing up this array. Force everyone to use the Show methods.

If you do this, you lose the pure virtual function. That means there is nothing for the derived class to override. And that means you have a design fault in your hierarchy.

So, if Shows have titles and Movies have genres and Plays have authers. Then Movies have no authers and Plays have no genres, which is patently false.

A Movie HAS-A author
A Movie HAS-A genre
A Movie HAS_A title
A Play HAS-A author
A Play HAS-A genre
A PLay HAS-A title

Now then moving the common processing to a base class:

A ShowHAS-A author
A Show HAS-A genre
A Show HAS_A title

this leaves you with the Show class having three data members. Plus six member functions to set/get these pieces of data.

You really do not have a Movie or a Play unless you have something in the Movie that is not an author, genre, or title. Ditto for Play.

The way it looks now, you have one class: Show.
Aug 1 '07 #5

RRick
Expert 100+
P: 463
Going back to your original question: Can you save C++ objects as binary data? The general answer is no. This is especially true if you are using virtual methods.

When you create a virtual method, each C++ object also stores a pointer (hidden somewhere in the object) to the virtual lookup table. The VLTable pointer is dependent on the specific program and can't be passed around to other programs.

The binary technique will "probably" work with very simple objects but you can forget about any objects that contain virtuals, STL, any data pointers and probably inheritance. Saving objects as binary data is a great idea, but there are too many caveats to make it worthwhile.

The "correct" way to save the object is through the design concept called persistance. In this case, you have to write your own read and write methods that maniputlate the data. There are some OOP design patterns that help to make this an easy task, but you stilll need to implement it yourself.
Aug 1 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.