C++编程中指针的声明与基本使用讲解

2020-01-06 14:31:32丽君


// const_pointer.cpp
int *const cpObject = 0;
int *pObject;

int main() {
pObject = cpObject;
cpObject = pObject;  // C3892
}

以下示例显示了当有指针指向某个指向对象的指针时如何将对象声明为 const。


// const_pointer2.cpp
struct X {
  X(int i) : m_i(i) { }
  int m_i;
};

int main() {
  // correct
  const X cx(10);
  const X * pcx = &cx;
  const X ** ppcx = &pcx;

  // also correct
  X const cx2(20);
  X const * pcx2 = &cx2;
  X const ** ppcx2 = &pcx2;
}


注:相关教程知识阅读请移步到C++教程频道。