g****y 发帖数: 436 | 1 搞不明白怎么回事。。。
指针不能自动做const 转换吗?
// file_func.cpp
namespace const_test{
void const_para(const int para) {}
void const_para(const int para, const char** pppara ) {}
}
int main(int argc, char** argv){
const int c_argc = argc; //ok. does not harm
const_test::const_para(c_argc);//ok. type match
const_test::const_para(1); //ok. type match
const_test::const_para(argc); //ok. auto type conversion.
const_test::const_para(para, argv); //error. Can not convert from
//'char**' to 'const char**'
} | a9 发帖数: 21638 | 2 好像跟编译器有关。
【在 g****y 的大作中提到】 : 搞不明白怎么回事。。。 : 指针不能自动做const 转换吗? : // file_func.cpp : namespace const_test{ : void const_para(const int para) {} : void const_para(const int para, const char** pppara ) {} : } : int main(int argc, char** argv){ : : const int c_argc = argc; //ok. does not harm
| h*******s 发帖数: 8454 | 3 说来话长,不过用const_cast就可以了
【在 g****y 的大作中提到】 : 搞不明白怎么回事。。。 : 指针不能自动做const 转换吗? : // file_func.cpp : namespace const_test{ : void const_para(const int para) {} : void const_para(const int para, const char** pppara ) {} : } : int main(int argc, char** argv){ : : const int c_argc = argc; //ok. does not harm
| t****t 发帖数: 6806 | 4 T* can be implicitly converted to const T*. but don't push it too far: T**
can not be implicitly converted to const T**. T** can be implicitly
converted to (T* const *). the standard specifically forbid this [4.4]:
[ Note: if a program could assign a pointer of type T** to a pointer of type
const T** (that is, if line #1
below were allowed), a program could inadvertently modify a const object (as
it is done on line #2). For
example,
int main() {
const char c = ’c’;
char* pc;
const char** pcc = &pc; // #1: not allowed
*pcc = &c;
*pc = ’C’; // #2: modifies a const object
}
—end note ]
【在 g****y 的大作中提到】 : 搞不明白怎么回事。。。 : 指针不能自动做const 转换吗? : // file_func.cpp : namespace const_test{ : void const_para(const int para) {} : void const_para(const int para, const char** pppara ) {} : } : int main(int argc, char** argv){ : : const int c_argc = argc; //ok. does not harm
| l*********s 发帖数: 5409 | 5 Relying on these shortcuts is good for nothing. Put yourself in the shoes of
reviewers, will you be impressed by the smartness of the coder, or rather
got really annoyed?
If you mean something, please state it clearly
type
as
【在 t****t 的大作中提到】 : T* can be implicitly converted to const T*. but don't push it too far: T** : can not be implicitly converted to const T**. T** can be implicitly : converted to (T* const *). the standard specifically forbid this [4.4]: : [ Note: if a program could assign a pointer of type T** to a pointer of type : const T** (that is, if line #1 : below were allowed), a program could inadvertently modify a const object (as : it is done on line #2). For : example, : int main() { : const char c = ’c’;
| t****t 发帖数: 6806 | 6 他问为什么这样不行, 我跟他说这样就是不行的, 因为标准说不行, 所以编译器说不行
. 标准为什么说不行? 因为如果行, 那有人就会钻如此这般的空子给编译器带来困扰.
这里既没有reviewer, 也没有coder, 也没有人要"relying on these shortcuts".
我自己什么意思也没有. 顺便说一下, 麻烦你看清楚我说的是什么.
of
【在 l*********s 的大作中提到】 : Relying on these shortcuts is good for nothing. Put yourself in the shoes of : reviewers, will you be impressed by the smartness of the coder, or rather : got really annoyed? : If you mean something, please state it clearly : : type : as
| g****y 发帖数: 436 | 7 谢谢thrust!
type
as
【在 t****t 的大作中提到】 : T* can be implicitly converted to const T*. but don't push it too far: T** : can not be implicitly converted to const T**. T** can be implicitly : converted to (T* const *). the standard specifically forbid this [4.4]: : [ Note: if a program could assign a pointer of type T** to a pointer of type : const T** (that is, if line #1 : below were allowed), a program could inadvertently modify a const object (as : it is done on line #2). For : example, : int main() { : const char c = ’c’;
| g****y 发帖数: 436 | 8 谢谢hatemaths!
【在 h*******s 的大作中提到】 : 说来话长,不过用const_cast就可以了
|
|