我不确定它的真正含义const vector<int *>所以我编写了下面的代码来获得一个想法但现在更加困惑.
vector<int *> v;
int x = 1, y = 2;
v.push_back(&x);
v.push_back(&y);
const vector<int *> w = v;
w[0] = &y; //failed. Element is a constant pointer?
*(w[0]) ++; //failed. Element pointer references to constant value?
Run Code Online (Sandbox Code Playgroud)
如果我在这里停下来,我会假设这const vector<int *>是一个向量const int * const,但后来我尝试了以下与这个假设明显矛盾的方法.
*(w[0]) += 3; //passed. Value not constant?
*(w[0]) = 20; //passed. Why...
Run Code Online (Sandbox Code Playgroud)
现在,*(w[0])对于未知的原因我明明对待++,并+=和分配方式不同.我确信自己const vector只声明了vector类的一个常量对象,并且上面的结果可能取决于运算符重载vector类的实际实现.但我无法绕过这个.有人可以帮忙解释一下吗?
如果它是相关的,我在Mac上使用g ++ 4.2.