0

The problem i have is this one. I have an array of type Event (Event is the base class of class Conference). In the array i put 5 objects (three of type Event and 2 from Conference in my case). In the exercise I am asked to create two binary files, one for objects of type Event and one for objects of type Conference. Next I have to check the type of the objects in the array, if it is from type Event it is written in Events.bin(binary file for objects of type event) if they are of type Conference they are written in Conference.bin. For that i use typeid() but when i read the files, instead of three objects in Events.bin i have all five there, the same goes for the other file. Here is the code i use to check the type and write them in the file:

for (int k = 0; k < N; k++) {
        if (typeid(arr[k]) == typeid(Conference)) {
            boutconferences.write((char*)&arr[k], sizeof(Conference));

        }else {
            boutevents.write((char*)&arr[k], sizeof(Event));
        }
    }

Second attempt:

 for (int k = 0; k < N; k++) {
            if (typeid(arr[k]) == typeid(Conference)) {
                boutconferences.write((char*)&arr[k], sizeof(Conference));
            }
            if(typeid(arr[k])==typeid(Event)) {
                boutevents.write((char*)&arr[k], sizeof(Event));
            }
        }

Both do the same

Thanks in advance.

0 Answers0