为什么我会参与此代码:
void foo ( const int ** );
int main() {
int ** v = new int * [10];
foo(v);
return 0;
}
此错误:
invalid conversion from ‘int**’ to ‘const int**’ [-fpermissive]|
我认为可以从非const转换为const。
答案 0 :(得分:15)
这是因为您尝试转换int** to const int**
int ** v = new int * [10]; // v is int**
foo(v); //but foo takes const int**
int **
是:“指向整数的指针”。const int **
是:“指向常量整数的指针”。使用const
是一份合同,你不能通过两个引用的间接来满足这个合同。
来自标准:
const char c = 'c';
char* pc;
const char** pcc = &pc; // not allowed (thankfully!)
^^^ here the bundit is hidden under const: "I will not modify"
*pcc = &c; // *pcc is "pointer to const" right? so this is allowed...
*pc = 'C'; // would allow to modify a const object, *pc is char right?
因此可以修改const char
始终,只需使用上面的过程即可。
还有:
char *s1 = 0;
const char *s2 = s1; // OK...
char *a[MAX]; // aka char **
const char * const*ps = a; // no error!
从以下链接中引用:
通过类比的方式,如果你在合法的伪装下隐藏罪犯,他 然后可以利用对伪装的信任。那很糟糕。
http://www.parashift.com/c++-faq-lite/constptrptr-conversion.html
与此相关的也是无效的转化Derived** → Base**
。如果转换Derived** → Base**
是合法的,则可以取消引用Base**
(产生Base*
),并且可以使Base *指向不同派生类的对象,可能会导致严重问题。了解原因:
class Vehicle {
public:
virtual ~Vehicle() { }
virtual void startEngine() = 0;
};
class Car : public Vehicle {
public:
virtual void startEngine();
virtual void openGasCap();
};
class NuclearSubmarine : public Vehicle {
public:
virtual void startEngine();
virtual void fireNuclearMissle();
};
int main()
{
Car car;
Car* carPtr = &car;
Car** carPtrPtr = &carPtr;
Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++
NuclearSubmarine sub;
NuclearSubmarine* subPtr = ⊂
*vehiclePtrPtr = subPtr;
// This last line would have caused carPtr to point to sub !
carPtr->openGasCap(); // This might call fireNuclearMissle()!
...
}
http://www.parashift.com/c++-faq-lite/derivedptrptr-to-baseptrptr.html
考虑:
class Vehicle {
public:
virtual ~Vehicle() { }
virtual void startEngine() = 0;
};
class Car : public Vehicle {
public:
virtual void startEngine(){printf("Car engine brummm\n");}
virtual void openGasCap(){printf("Car: open gas cap\n");}
virtual void openGasCap2(){printf("Car: open gas cap2\n");}
virtual void openGasCap3(){printf("Car: open gas cap3\n");}
virtual void openGasCap4(){printf("Car: open gas cap4\n");}
};
class NuclearSubmarine : public Vehicle {
public:
int i;
virtual void startEngine(){printf("Nuclear submarine engine brummm\n");}
virtual void fireNuclearMissle3(){printf("Nuclear submarine: fire the missle3!\n");}
virtual void fireNuclearMissle(){printf("Nuclear submarine: fire the missle!\n");}
virtual void fireNuclearMissle2(){printf("Nuclear submarine: fire the missle2!\n");}
};
int main(){
Car car; Car* carPtr = &car;
Car** carPtrPtr = &carPtr;
//Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++, But:
Vehicle** vehiclePtrPtr = reinterpret_cast<Vehicle**>(carPtrPtr);
NuclearSubmarine sub; NuclearSubmarine* subPtr = ⊂
*vehiclePtrPtr = subPtr; // carPtr points to sub !
carPtr->openGasCap(); // Nuclear submarine: fire the missle3!
carPtr->openGasCap2(); // Nuclear submarine: fire the missle!
carPtr->openGasCap3(); // Nuclear submarine: fire the missle2!
//carPtr->openGasCap4(); // SEG FAULT
}
答案 1 :(得分:7)
如果你在cv资格认证的第一个差异中添加了所有级别的const,那么你只能在类似的指针类型之间的转换中添加const限定。
因此,您可以将int**
转换为int const* const*
,但不能转换为int const* *
。如果允许在中间级别省略添加const,则可以执行以下操作:
const int c = 29;
int *pi;
const int** ppci = π // only adding const, right
*ppci = &c;
*pi = 0; // changing c ?! but no const_cast in sight
答案 2 :(得分:3)
你被C ++对指针的令人困惑的解析规则误导了。看这种方式可能更清楚:
typedef const int * ptr_to_const_int;
void foo( ptr_to_const_int *);
int main() {
int ** v = new int * [10];
foo(v);
return 0;
}
foo()的参数列表承诺的是,你将向它传递一个指针(指向常量的东西)。但是新的int * [10]意味着“指向(指向非常量事物)”。
所以如果foo定义如下:
foo( const int **p )
{
(*p); //<-- this is actually of type const int *
}
虽然我认为你期待
foo( const int **p )
{
(*p); //<-- you're expecting this to be of type int *
p = 0; //<-- and this to throw a compiler error because p is const
}
但并不是说你宣称自己是不变的,而是它指向的东西。
无论如何只需在这种情况下使用typedef,一切都清晰可读。