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! | | | | re: Strange problem with storing inherited classes - HELP!
"Gandu" <cpptutor2000@yahoo.com> wrote in message
news:3c7c9804.0410161955.671f8254@posting.google.c om...[color=blue]
> 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[/color]
*);[color=blue]
> ~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![/color]
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 | | | | re: Strange problem with storing inherited classes - HELP!
"Gandu" <cpptutor2000@yahoo.com> wrote in message
news:3c7c9804.0410161955.671f8254@posting.google.c om...[color=blue]
> 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![/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|