Hi,
I'm quite new to c++ programming, so my concepts are not so increased yet...
I wanna to create a list to store my objects. I'll resume them here:
Code:
class Event { //I tried to built this class like an "interface" on java
public:
Event(int timeout); //on .cpp I'll do: this->timeout = timeout
virtual ~Event();
private:
int timeOut; //event timeout - all Events will have one
public:
int getTimeout();
//The message is particular to each Event
virtual int getMessage(char* message) = 0; //pure virtual method
}
class EventA : public Event {
public:
EventA(int timeout);
~EventA();
private:
int attributeOne;
int attributeTwo;
public:
int getMessage(char* message);
//this is implemented on cpp:
/* int EventA::getMessage(char* message) {
* strcpy(message, "EventA msg");
* }
*/
}
class EventB : public Event {
public:
EventB(int timeout);
~EventB();
private:
int attributeOne;
bool attributeTwo;
char attributeThree[128];
public:
int getMessage(char* message);
//this is implemented on cpp:
/* int EventB::getMessage(char* message) {
* strcpy(message, "EventB msg");
* }
*/
}
...
And what I want to do is an event list:
Code:
Event** eventList = new Event*[5];
eventList[0] = new EventA(4);
eventList[1] = new EventB(6);
eventList[2] = new EventC(8);
eventList[3] = new EventD(1);
eventList[4] = new EventE(2);
char temp[128] = "";
eventList[0]->getMessage(temp);
printf("%s\n", temp);
eventList[1]->getMessage(temp);
printf("%s\n", temp);
eventList[2]->getMessage(temp);
printf("%s\n", temp);
...
Sometimes this works and sometimes I got "segmentation fault".
I think I'm doing it wrong, cause these objects have different sizes, so I don't know if I could have all of them in a single array.
Someone have any tip or tutorial or how to to share with me?
Thanks in advance!