STL container assignment and const pointers

前端 未结 8 1791
时光取名叫无心
时光取名叫无心 2020-12-29 11:52

This compiles:

int* p1;
const int* p2;
p2 = p1;

This does not:

vector v1;
vector v2;
v2 = v1;         


        
8条回答
  •  既然无缘
    2020-12-29 12:24

    Dangerous, unless you know the types are absolutely compatible:

    v2 = reinterpret_cast & >(v1);

    Most STL implementations do use a specialization were all vectors of pointers share the same underlying implementation. This is since (void *) is typically the same size as (int *) or any other pointer type.

提交回复
热议问题