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

C++ 已棄用從字符串常量到“char*"的轉換

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

問題描述

我有一個帶有 private char str[256];

為此我有一個顯式構造函數:

and for it I have an explicit constructor:

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

我稱之為:

myClass obj("example");

當我編譯它時,我收到以下警告:

When I compile this I get the following warning:

不推薦將字符串常量轉換為 'char*'

deprecated conversion from string constant to 'char*'

為什么會這樣?

推薦答案

當您遇到以下情況時,您會看到一條錯誤消息:

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

char* pointer_to_nonconst = "string literal";

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

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 的正確性,您在程序中的某處缺少一個或多個 const .但是您向我們展示的代碼不是問題,因為它沒有進行這種已棄用的轉換.警告一定來自其他地方.

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.

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

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

相關文檔推薦

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++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 一本久 | 国产精品va | 欧美黄色一区二区 | 97国产精品人人爽人人做 | 久久久久女人精品毛片九一 | 色在线视频 | 99视频网站 | 国产美女av | 久久久综合视频 | 国产一级免费 | 欧美日韩国 | av噜噜 | 国产免费观看视频 | 久久久精品免费 | 欧美黄色一级视频 | 久久视频在线免费观看 | 日韩欧美专区 | 天堂成人在线 | 日韩av网站在线观看 | 欧美日皮视频 | 蜜臀久久99精品久久久久久宅男 | 色综合五月天 | 欧美日韩一区二区在线 | 黄色成人在线视频 | 台湾av在线 | 国产一级18片视频 | 伊人久久免费视频 | 人人射人人| 99re在线观看视频 | 亚洲激情网站 | 日本少妇网站 | 日韩一级免费视频 | 欧美日韩国产一区二区 | 国产中文字幕一区 | av免费观看网址 | 国产精品一区二区av | a视频在线免费观看 | 久插视频| 欧美国产日韩在线 | 国产小视频在线播放 | 欧美一区二区在线 |