<pre class="replyask-text" id="content-29911763" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; zoom: 1; background-color: rgb(255, 255, 255);">const char* = char*
char*转const char* 可以转。发过来不行
</pre><pre class="replyask-text" id="content-29911763" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; zoom: 1; background-color: rgb(255, 255, 255);">去掉const属性方法如下:
string str = "aaa";
char* p =const_cast<char*>(str.c_str());
printf("%s",p);
</pre><pre class="replyask-text" id="content-29911524" name="code" style="white-space: pre-wrap; word-wrap: break-word; font-size: 14px; margin-top: 0px; margin-bottom: 0px; padding: 0px; font-family: Arial; zoom: 1; line-height: 24px; background-color: rgb(255, 252, 246);">const_cast用来丢弃变量的const声明,但不能改变变量所指向的对象的const属性。即:const_cast用于原本非const的对象;如果用于原本const的对象,结果不可预知(C++语言未对此种情况进行规定),请注意我上面用下划线标示的“变量”vs“对象”这个要自己慢慢体会。
一般情况下const_cast是用于这种情形:const指针(变量)指向非const对象,程序员确认这一点(所指向的对象非const)时,使用const_cast操作符丢弃变量的const修饰获得一个非const指针,详见《The C++ Programming language(special edition)》第15.4.2.1节。