generics - C++ comparing pointers to different types? -


i'm having hard time finding information these kind of stuff! :(

i'm confused why isn't working:

vector<b*> b; vector<c*> c; (b , c subclasses of a)  (both initialized , contain elements etc etc...)   template <class first, class second> bool func(vector<first*>* vector1, vector<second*>* vector2)    return vector1 == vector2;  

when compiling returns:

types pointed unrelated; conversion requires reinterpret_cast, c-style cast or function-style cast 

i don't see why wouldn't work, pointers hold addresses yeah? why don't compare if 2 vector pointers... point same address(-es)?

here's simple example you're asking won't work.

struct a{ int i; }; struct ohnoes { double d; }; struct b: public {}; struct c: public ohnoes, public b {}; 

so here, b , c both subclasses of a. however, instance of c unlikely have same address b subobject.

that is, this:

c c; b *b = &c; // valid upcast assert(static_cast<void*>(b) == static_cast<void *>(&c)); 

will fail.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -