Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an event struct like that:
C++
struct Event
{
    public:
        double time;
        Ball partners[2];
};

I check collision times of pool balls. I'm adding all possible events to a list. If partners were only balls it would be easy. However i need to check edges of table. So i think i need something like that:
C++
Partner partners[2];

But how? If I inherit both ball and edge class from a partner class I loose information when transferring back.

Any suggestion?
Posted

1 solution

The alternative is to make Event a base class and derive multiple types of events. The base class would just contain the time member and perhaps an event type. The derived classes bring in the rest.

Don't forget to make the destructor of that Event base class virtual!
 
Share this answer
 
Comments
maxc900 10-Jul-13 8:17am    
but can I add to my eventlist different types of events?

and this may be a silly question but why virtual destructor? i'm just a scientist away from c++ :)
nv3 10-Jul-13 8:38am    
Yes, you can add multiple event types to your event list, as long as they are all derived from your base class Event. And all general code that just deals with arbitrary events from your event list would use virtual functions of the Event class to perform operations. For example, one of the virtual functions you might want to define is a GetEventName() function that returns some kind of type-specific name. You would then override this virtual function in each of your derived classes and return the desired name for that class. That whole concept is call polymorphy and is one of the key features of object oriented languages like C++.

The destructor of a class that is intended to be derived from, i.e. a base class, should have a virtual destructor. The reason is that all your derived classes will have different length and will need to do different things in their destructors. For example, your EventBalls class will need to call the destructor for the embedded Ball object, and EventTable's destructor would need to call some other destructors. Therefore, you should tell your base class that the destructor function is virtual and that the respective destructor of the derived class needs to be called.
maxc900 10-Jul-13 8:44am    
This is gold for me. I appreciate your help. Thank you very much.
nv3 10-Jul-13 9:03am    
You are welcome!
maxc900 10-Jul-13 10:44am    
i'll disturb you once again.

i did what you told me. my ball_event and edge_event derive from event class. I made an eventlist (list<event>). I can add both ball_events and edge_events to that list however I can access only members of event class (type and time). I cannot determine which are partners.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900