简体   繁体   English

如何使 MinGW 将诸如“文本”之类的文本视为 const char*

[英]How to make MinGW to treat texts like “text” as const char*

AFAIK, after upgrading to MinGW 4.4. AFAIK,升级到MinGW 4.4 后。 * some time ago all of my literally written strings (like "i'm the string" ) are treated as of the type std::string . * 前段时间,我所有的字面上写的字符串(如"i'm the string" )都被视为std::string类型。 Is there any command line option to make MinGW treat them as const char* as it was before?是否有任何命令行选项可以让 MinGW 像以前一样将它们视为const char*

Strings are per default char * / char[] in C / C++, only if you explicit say its a std::string you'll get those.字符串在 C / C++ 中默认为char * / char[] ,只有当你明确说它是std::string时,你才会得到这些。

Example:例子:

std::cout << "first:\t" << typeid("aa").name() << std::endl;
std::cout << "second:\t" << typeid(std::string("a")).name() << std::endl;

Output: Output:

first:  A3_c
second: Ss

Result:结果:

  • first: a char array with length of 3 (= 2 chars + end)第一个:长度为 3 的char数组(= 2 chars + end)
  • second: is a std::string第二:是一个std::string

As you can see, if you write "abc" you wont get a std::string .如您所见,如果您写"abc" ,您将不会得到std::string

But: if you write std::string str = "abc" you get a std::string because the asignment operator ( = ) is used: string& operator= (const char* s)但是:如果你写std::string str = "abc"你会得到一个std::string因为使用了赋值运算符( = ): string& operator= (const char* s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM