English 中文(简体)
C-Style strings to setd:string identification
原标题:C-Style strings to std::string conversion clarification

我认为,有C++经验的人很容易回答几个问题,我 bold笑TL的问道。

Given the following code:

void stringTest(const std::string &s)
{
    std::cout << s << std::endl;
}

int main()
{
    stringTest("HelloWorld");
}

Hopefuly someone can point out the error in my thought process here:

www.un.org/spanish/ecosoc 参数为何在显示 测试在通过《聚乙烯法》时必须贴上标记。 Is 暗中转成 st子:使用其模范构造进行扼杀,因此,“......”不再指字面(也无需混淆)。

www.un.org/spanish/ecosoc 此外,模范构造者喜欢什么,汇编者在看到

stringTest("HelloWorld");

它是否只是承认一个有理的字面像一个char子一样?

在研究影印机时,我 st弄了这些问题。 还有一个快速的问讯,供我自己澄清。

在类似情况下:

std::string s = "HelloWorld";

Is the cstyle string constructor used to instantiate a temporary std::string, and then the temporary string is copied into "s" using the string copy constructor?:

std::string(const std::string&);
最佳回答

Why does the parameter in stringTest have to be marked const when passed a C-Style string?

It only has to when the parameter is a reference, since a temporary std::string is constructed from the char const* you pass in and a non-const reference to a temporary is illegal.

它是否只是承认一个有理的字面像一个char子一样?

缩略语为<代码>char const阵列,代号为char const*。 因此,汇编者推断,它应当使用非<代码>explicit Constructionor std:string:string(char const *)建造临时人员。

模范构造者是否被用来当下临时 st:铺设,然后使用铺面复印机将临时铺设为“s”?

它比这更加复杂。 是的,临时设立。 但是,复印机可能或可能不会被叫到;编辑员可以把复印件施工作为优化。 仍须提供复印机,不过,以下几张成像集:

class String {
    String(char const *) {}
  private:
    String(String const &);
};

int main()
{
    String s = "";
}

此外,在C++11中,如果提供,将使用搬迁建筑商;在这种情况下,不需要复印建筑商。

问题回答

Does it simply recognize a string literal to be something like a char*?

与我一样,原问题的这一部分没有得到明确回答。 我完全赞成(并高发)日arian答。

从根本上说,如果编辑在守则中看到一个明显字面的话,你需要理解他们正在做什么。 这种形形形色色的果园(实际上属于任何一类)实际上被储存在一个与其构成的法典完全不同的地点(视结构而定,数字字面可以储存在地点本身,作为大会/综合指示的一部分)。 这里的法典的两个部分是“更多或更少”等同(缺乏包括或称号的):

int main(void)
{
    cout << "Hello!" << endl;
    return 0;
}

这更接近“真正”的情况:

const char HELLO_STR[] = {  H ,  e ,  l ,  l ,  o ,  ! , 0 };

int main(void)
{
    cout << HELLO_STR << endl;
    return 0;
}

如果我错过阵列或不管怎么样,那么我就认为这反映了我所说的“直面”存放地点。 它不是在线的,而是它所定义的方案的另一部分不可想象的固定。 此外,一些(大多数是吗?)的汇编者还安排了“连续”字面,这样,如果你在50个地方使用同一字面,就只能储存其中之一,而且所有这些都重复了同样的常态,保存记忆。

因此,你记得,在你使用直面字面时,你会再次使用“明显”某些地方存在的“最合点”,这被暗地转化为混凝土。

Why does the parameter in stringTest have to be marked const when passed a C-Style string?

EDIT: Temporaries must be immutable. See larsmans comment and answer, he is right.

简单理由:

void change(std::string& c) { c = "abc"; }
change("test"); // what should the code exactly do??

此外,模范构造者喜欢什么,汇编者知道如何在看到时援引这一点:

It looks up std::string for string(char*) constructor

在类似情况下:

std::string s = "HelloWorld";

Is the cstyle constructor used to instantiate a temporary std::string, and then the temporary string is copied into "s" using the string copy constructor?: std::string(const std::string&);

页: 1 在这种确切情况下(TYPE变量=SOMETHING),其与写作TYPE变量(SOMETHING);相同。 因此,没有复制。

The const string & s is in this example required to consider the Constructionor from the debate “HelloWorld”。 使用的构造是改型建筑。

A string & s/2006/2 t do,因为s是直接指指引标的。

类型转换的定义是类似于某种类型

 basic_string(const _CharT* __s);

a. 类型

typedef basic_string<char>    string; 

因此,声明将评估

basic_string(const char * __s)    




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签