久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 已棄用從字符串常量到“char*"的轉(zhuǎn)換

C++ deprecated conversion from string constant to #39;char*#39;(C++ 已棄用從字符串常量到“char*的轉(zhuǎn)換)
本文介紹了C++ 已棄用從字符串常量到“char*"的轉(zhuǎn)換的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個(gè)帶有 private char str[256];

為此我有一個(gè)顯式構(gòu)造函數(shù):

and for it I have an explicit constructor:

explicit myClass(const char *func)
{
    strcpy(str,func);
}

我稱之為:

myClass obj("example");

當(dāng)我編譯它時(shí),我收到以下警告:

When I compile this I get the following warning:

不推薦將字符串常量轉(zhuǎn)換為 'char*'

deprecated conversion from string constant to 'char*'

為什么會(huì)這樣?

推薦答案

當(dāng)您遇到以下情況時(shí),您會(huì)看到一條錯(cuò)誤消息:

This is an error message you see whenever you have a situation like the following:

char* pointer_to_nonconst = "string literal";

為什么?好吧,C 和 C++ 在字符串文字的類型上有所不同.在 C 中,類型是字符數(shù)組,而在 C++ 中,它是 constant 字符數(shù)組.在任何情況下,都不允許更改字符串字面量的字符,因此 C++ 中的 const 并不是真正的限制,而是更多的類型安全.出于安全原因,如果沒有顯式轉(zhuǎn)換,從 const char*char* 的轉(zhuǎn)換通常是不可能的.但是為了與 C 向后兼容,C++ 語言仍然允許將字符串文字分配給 char* 并警告您此轉(zhuǎn)換已被棄用.

Why? Well, C and C++ differ in the type of the string literal. In C the type is array of char and in C++ it is constant array of char. In any case, you are not allowed to change the characters of the string literal, so the const in C++ is not really a restriction but more of a type safety thing. A conversion from const char* to char* is generally not possible without an explicit cast for safety reasons. But for backwards compatibility with C the language C++ still allows assigning a string literal to a char* and gives you a warning about this conversion being deprecated.

因此,為了 const 的正確性,您在程序中的某處缺少一個(gè)或多個(gè) const .但是您向我們展示的代碼不是問題,因?yàn)樗鼪]有進(jìn)行這種已棄用的轉(zhuǎn)換.警告一定來自其他地方.

So, somewhere you are missing one or more consts in your program for const correctness. But the code you showed to us is not the problem as it does not do this kind of deprecated conversion. The warning must have come from some other place.

這篇關(guān)于C++ 已棄用從字符串常量到“char*"的轉(zhuǎn)換的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺(tái)輸出?)
How do I calculate the week number given a date?(如何計(jì)算給定日期的周數(shù)?)
OpenCV with Network Cameras(帶有網(wǎng)絡(luò)攝像機(jī)的 OpenCV)
Export all symbols when creating a DLL(創(chuàng)建 DLL 時(shí)導(dǎo)出所有符號(hào))
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 日韩av三区 | 欧美色999| 国产精品1区 | 黑人一级片视频 | 欧美8一10sex性hd | 又黄又爽的网站 | 91av视频在线观看 | 精品99在线 | 成人久草| 玖玖国产 | 色悠悠久 | 日韩a| 久久久青草婷婷精品综合日韩 | 久久成人精品视频 | 高清久久 | 亚洲一区二区三区免费观看 | 天天操欧美 | 欧美精品成人一区二区三区四区 | 亚洲毛片在线观看 | 欧美日韩不卡 | 婷婷成人在线 | 久久99蜜桃综合影院免费观看 | 久久偷人| 男人的天堂在线视频 | 午夜免费av | 国产精品久久性 | 成人久久久| 香蕉视频91| 波多野结衣一二三区 | 黑人精品欧美一区二区蜜桃 | 91不卡在线 | 欧美一级片在线播放 | 欧美福利视频 | 成人在线a | 中文字幕第5页 | 99视频在线免费观看 | 午夜视频在线观看一区二区 | www.日韩 | 欧美一区二区三区在线观看视频 | 男女国产视频 | 国产一区二区精品在线观看 |