473,396 Members | 1,834 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,396 software developers and data experts.

Strange problem with storing inherited classes - HELP!

Could some C++ guru shed some light as to what might be going on?
I have the following two classes which are simply data containers:

const unsigned int MAXSIZE = 40;

class DataItem{
char fname[MAXSIZE];
char lname[MAXSIZE];
char fname[MAXSIZE];
char street[MAXSIZE];
char city[MAXSIZE];
char county[MAXSIZE];
char state[MAXSIZE];
public:
DataItem();
DataItem(char *, char *, char *, char *, char *, char *);
~DataItem(){}
void showData();
};

class DataItem1: public DataItem{
char zipcode[MAXZIZE];
char employeeId[MAXSIZE];
public:
DataItem1();
DataItem1(char *, char *, char *, char *, char *, char *, char *, char *);
~DataItem1(){}
void showData1();
};

In a seperate class I write to a file using binary io using:
template <class T>
void FileWriter<T>::writeData(DataItem &t){
out.write((reinterpret_cast<char *>(&t)), sizeof(t));
}

Similarly, there is a way to read in the data using:
template <class T>
void FileReader<T>::readData(DataItem &t){
in.read((reinterpret_cast<char *>(&t)), sizeof(t));
}

Contents of the data objects can be also be displayed.
The problem that I am having is that if I create two objects,
one of type DataItem and the other of type DataItem1, and then
try to write(to the same file) and read in the same data(from
the same file), I find that the last two data members of any
DataItem1 object ALWAYS contains junk values. Could someone please
point out what might be going wrong? Thanks in advance for your
help!
Jul 22 '05 #1
2 1511

"Gandu" <cp**********@yahoo.com> wrote in message
news:3c**************************@posting.google.c om...
Could some C++ guru shed some light as to what might be going on?
I have the following two classes which are simply data containers:

const unsigned int MAXSIZE = 40;

class DataItem{
char fname[MAXSIZE];
char lname[MAXSIZE];
char fname[MAXSIZE];
char street[MAXSIZE];
char city[MAXSIZE];
char county[MAXSIZE];
char state[MAXSIZE];
public:
DataItem();
DataItem(char *, char *, char *, char *, char *, char *);
~DataItem(){}
void showData();
};

class DataItem1: public DataItem{
char zipcode[MAXZIZE];
char employeeId[MAXSIZE];
public:
DataItem1();
DataItem1(char *, char *, char *, char *, char *, char *, char *, char *); ~DataItem1(){}
void showData1();
};

In a seperate class I write to a file using binary io using:
template <class T>
void FileWriter<T>::writeData(DataItem &t){
out.write((reinterpret_cast<char *>(&t)), sizeof(t));
}

Similarly, there is a way to read in the data using:
template <class T>
void FileReader<T>::readData(DataItem &t){
in.read((reinterpret_cast<char *>(&t)), sizeof(t));
}

Contents of the data objects can be also be displayed.
The problem that I am having is that if I create two objects,
one of type DataItem and the other of type DataItem1, and then
try to write(to the same file) and read in the same data(from
the same file), I find that the last two data members of any
DataItem1 object ALWAYS contains junk values. Could someone please
point out what might be going wrong? Thanks in advance for your
help!


You're reading and writing sizeof(DataItem) bytes. Why do you
expect to see sizeof(DataItem1) bytes in your file?

Research the term 'slicing'.

-Mike
Jul 22 '05 #2

"Gandu" <cp**********@yahoo.com> wrote in message
news:3c**************************@posting.google.c om...
Could some C++ guru shed some light as to what might be going on?
I have the following two classes which are simply data containers:

const unsigned int MAXSIZE = 40;

class DataItem{
char fname[MAXSIZE];
char lname[MAXSIZE];
char fname[MAXSIZE];
char street[MAXSIZE];
char city[MAXSIZE];
char county[MAXSIZE];
char state[MAXSIZE];
public:
DataItem();
DataItem(char *, char *, char *, char *, char *, char *);
~DataItem(){}
void showData();
};

class DataItem1: public DataItem{
char zipcode[MAXZIZE];
char employeeId[MAXSIZE];
public:
DataItem1();
DataItem1(char *, char *, char *, char *, char *, char *, char *, char
*);
~DataItem1(){}
void showData1();
};

In a seperate class I write to a file using binary io using:
template <class T>
void FileWriter<T>::writeData(DataItem &t){
out.write((reinterpret_cast<char *>(&t)), sizeof(t));
}

Similarly, there is a way to read in the data using:
template <class T>
void FileReader<T>::readData(DataItem &t){
in.read((reinterpret_cast<char *>(&t)), sizeof(t));
}

Contents of the data objects can be also be displayed.
The problem that I am having is that if I create two objects,
one of type DataItem and the other of type DataItem1, and then
try to write(to the same file) and read in the same data(from
the same file), I find that the last two data members of any
DataItem1 object ALWAYS contains junk values. Could someone please
point out what might be going wrong? Thanks in advance for your
help!


You seem to think that sizeof(t) should be smart enough to work out what is
the runtime type of t, but it isn't.

Your code also has some other curious features, there is no good reason for
FileRead and FileWriter to be part of a template class. Perhaps what you
really want is

void FileWriter<T>::writeData(const T &t){
out.write((reinterpret_cast<const char *>(&t)), sizeof(T));
}

template <class T>
void FileReader<T>::readData(T &t){
in.read((reinterpret_cast<char *>(&t)), sizeof(T));
}

then you would get the right number of bytes in your file. But it is hard to
be sure given the code posted.

john

Jul 22 '05 #3

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

Similar topics

4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
4
by: Charles | last post by:
I am still having problems with sessions crossing over from other sessions. What I mean by this is that Mary may get just some or all of Renae’s information. I think that what is going on with...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
6
by: semedao | last post by:
Hi All, I had working code that made custom serialization on objects that inherit from queue in the inherited queue I create my own GetObjectData: public void GetObjectData(SerializationInfo info,...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
4
by: jeet232 | last post by:
Hi, I've these classes Integer derived from Object String derived from Object I'm creating another class MyMap which shall have the capability to store Integer, String (or any other obj...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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 project—planning, 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.