Pointing to elements in a vector.

Hi,

So I've got 3 structs - track, top_hit, and bot_hit. A track is essentially just a pointer to a top_hit and a bot_hit with some member functions to analyse them.

// Constructor for track:
track(top_hit *topHit_i, bot_hit *botHit_i) : topHit(topHit_i), botHit(botHit_i) {
x_delay = botHit->x_time - topHit->x_time;
y_delay = botHit->y_time - topHit->y_time;
}


In my main testing routine I make a top_hit called bruce and a bot_hit called giles. Then I make a track jerome out of them.

track jerome(&bruce,&giles);
jerome.Show();


This does exactly what I want it to do.

Now I am trying to convert this to work with a vector of hits as that ultimately will be condusive to what I want (but mostly because I need to learn how to use vectors).

I make my vector of hits, and test it...

vector <hit> hits;
hits.push_back(bruce);
hits.push_back(giles);

hits[0].Show();
hits[1].Show();


...which works fine.

Now I try to make jerome again, under a more casual name.

track jerry(&hits[0],&hits[1]);


This does not work. I've also tried track jerry(hits[0],hits[1]) and jerry(*hits[0],*hits[1]) but jerry is not having any of those things. I suspect this is because I am self taught and do not know how to use pointers very well.

Can anybody help me point at these vector elements properly?
Last edited on
You're using polymorphism, right? Well, casting of a derived type to base type is implicit, but you have to explicitly cast the base to a derived type.
Hm ok. So do I need two separate top_hit and bot_hit vectors? Or do I need to make a different constructor for track?
You can still use one vector, you just need to have a way that you can tell if the hit you are accessing is a top hit or bottom hit and then cast them to the correct types to pass to your tack constructor.
Topic archived. No new replies allowed.