[英]C++ non const to const casting compilation error
the below code doesn't compile 下面的代码无法编译
void aaa(const int **a) {
}
int *a[] = {new int[2]};
aaa(a);
I got "cannot convert parameter 1 from 'int [1]' to 'const int * " in VS2010 and similar error in gcc 我在VS2010中遇到“无法将参数1从'int [1]'转换为'const int * ”的问题,并且在gcc中出现了类似错误
when I change my declaration to: 当我将声明更改为:
int const *a[] = {new int[2]};
or 要么
const int *a[] = {new int[2]};
it compiles, but I don't understand why it doesn't accept a non const variable declaration 它可以编译,但是我不明白为什么它不接受非const变量声明
The type of a
is int*[]
; 的类型的a
是int*[]
; the type you want is int const**
. 您想要的类型是int const**
。 int*[]
converts to int**
, but this will not convert implicitly to int const**
. int*[]
转换为int**
,但这不会隐式转换为int const**
。 Consider the following code to understand why: 考虑以下代码以了解原因:
static int const ci = 42;
void aaa( int const** out )
{
*out = &ci;
}
int
main()
{
int* pa;
aaa( &pa ); // NOT LEGAL, because...
*pa = 0; // would now change ci
std::cout << ci << std::endl;
return 0;
}
As you can see, allowing this conversion would break const without requiring a cast. 如您所见,允许这种转换将破坏const而无需强制转换。
Depending on what you are doing, you might want to use: 根据您在做什么,您可能需要使用:
void aaa( int const* const* out );
The implicit conversion of int**
to int const *const *
is legal. 将int**
隐式转换为int const *const *
是合法的。 (Otherwise, you'll need a const_cast
somewhere, to tell the compiler that you know what you're doing, and that it isn't really a problem.) (否则,您将需要在某个地方使用const_cast
来告诉编译器您知道自己在做什么,并且这实际上不是问题。)
The function aaa
expects a pointer-to-pointer-to-constant-int. 函数aaa
期望有一个指向常量常数的指针。 Your variable a
is a pointer-to-pointer-to-int. 您的变量a
是指向整数的指针。 It is an error to assign the latter to the former. 将后者分配给前者是错误的。
both int const *a[]
and const int *a[]
is actually the same thing, matching the signature of aaa
. int const *a[]
和const int *a[]
实际上都是同一件事,匹配aaa
的签名。 If you tried int * const a[]
, that would be a different type (pointer-to-constant-pointer-to-int) and you would trigger the type error again. 如果您尝试使用int * const a[]
,那将是另一种类型(pointer-to-constant-pointer-to-int),并且您将再次触发类型错误。
If you want your function aaa
to take a constant-pointer-to-pointer-to-int, you need to write aaa(int ** const a)
, but having a const-ness on parameter values has actually no effect on what you can call with. 如果要让函数aaa
采用常量指针到指针到int,则需要编写aaa(int ** const a)
,但是对参数值保持常量实际上对您的内容没有影响可以打电话。
Edit: "But isn't constness added implicitly - done with an implicit cast? (Which is the actual question)" 编辑: “但是不是隐式添加常量性-用隐式强制转换完成吗?(这是实际的问题)”
Constness can be implicitly added to the value you are passing, eg 常量可以隐式添加到您传递的值中,例如
void aaa(const int a) {}
int b=5;
aaa(b);
... or one level pointer ...或一级指针
void aaa(const int* a) {}
int *b=new int;
aaa(b);
... but cannot be added deeper. ...但是不能加深。 For example this is invalid: 例如,这是无效的:
void aaa(const int** a) {}
int* b=new int;
int** c=&b;
aaa(c);
I think James Kanze explains it much better in his answer. 我认为詹姆斯·坎泽(James Kanze)在回答中会更好地说明这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.