Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vector of const pointers? [duplicate]

The following doesn't compile (very verbose error, but basically "cannot be overloaded" and "invalid conversion from 'const void*' to 'void*'"). I can understand why for example push_back() may not compile, as you can't copy/move into a Foo* const, but why doesn't this compile:

#include <vector>
using namespace std;

class Foo;

int main()
{
  vector<Foo* const> vec;
}
like image 616
Agrim Pathak Avatar asked Mar 31 '15 21:03

Agrim Pathak


1 Answers

You are declaring a vector that contain const pointer of Foo, that mean that the pointer could not be modified. When you insert an element in the vector would need to write to a const pointer (not valid).

You are sure it's not: std::vector<Foo const *> vec; where Foo don't be modified by the pointer could.

like image 111
NetVipeC Avatar answered Oct 01 '22 08:10

NetVipeC