久久久久久久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 入門)
主站蜘蛛池模板: 午夜久久 | 伊人久久艹 | 亚洲三级av| 亚洲高清视频在线 | 国产成人精品网站 | 亚洲高清视频在线 | 日韩欧美精品一区二区 | 中文字幕影院 | 中文字幕中文字幕 | 中文字幕二区 | 日韩少妇视频 | 国产精品欧美一区二区 | 国产视频一二区 | 国产xxx| 色一情一乱一乱一区91av | 九九在线观看高清免费 | 中文字幕免费在线观看 | 午夜视频免费看 | 一区二区在线视频 | 久热精品在线 | 欧美视频在线一区 | 天堂中文在线视频 | 天堂av影院 | 免费看一级黄色片 | 成人做爰www看视频软件 | 超碰在线小说 | 国产在线第一页 | 日本黄色中文字幕 | 日日撸夜夜操 | 国产精品嫩草影院桃色 | 天天综合视频 | 日韩激情久久 | 四虎影视库 | 99视频 | 国产伦精品一区二区三区照片 | 日本黄a三级三级三级 | 欧美在线视频一区 | 精品在线观看视频 | 伊人色播 | 奇米狠狠干| 欧美一区二区在线播放 |