1.So I have:
Class A;
Class B : public A;
Class C : public B;
2.And a vector of pointers of type B:
vector<B*> vec;
3.Then:
C* ptr = new C();
vec.push_back(ptr);
So the question is, it is reliable to use std::find like this?
std::find(vec.begin(), vec.end(), prt);
Also, is it fine to do search using this-> pointer?
std::find(vec.begin(), vec.end(), this); //inside of a type C object
Thanks in advance.
Yes, this is safe since there is a well-defined comparison (
==) between pointers to objects in a type hierarchy. Even though the actual value of the pointers may differ after a conversion to a base class type (often the case with multiple inheritance), the runtime is required to adjust for that so that comparisons between those pointers still yield the correct result.