C++;按引用传递字符串与按引用传递字符数组 P>好,所以我是C++的新手,我只想问为什么你不应该通过“和”符号来引用字符数组,但是你应该使用字符串,因为这两个参数都是指针。我编写的示例代码: void changeChar(char* buffer, int bSize) { strcpy_s(buffer, bSize, "test123"); } void changeString(string* buffer) { *buffer = "test321"; } char mychar[10] = "hello world"; string mystr; changeChar(mychar, sizeof(mychar)); changeString(&mystr);

C++;按引用传递字符串与按引用传递字符数组 P>好,所以我是C++的新手,我只想问为什么你不应该通过“和”符号来引用字符数组,但是你应该使用字符串,因为这两个参数都是指针。我编写的示例代码: void changeChar(char* buffer, int bSize) { strcpy_s(buffer, bSize, "test123"); } void changeString(string* buffer) { *buffer = "test321"; } char mychar[10] = "hello world"; string mystr; changeChar(mychar, sizeof(mychar)); changeString(&mystr);,c++,arrays,pointers,stdstring,C++,Arrays,Pointers,Stdstring,您需要知道,std::string不是内置类型。它是一个类,实现所有类型的自定义行为,例如在对象复制时创建硬拷贝 "some text" // this is a string literal, meaning the type is const char[num] 当您输入字符串文字时,它很可能位于名为“.rodata”(只读数据)的代码段内。不能合法修改此字符的值。文本还有一个“空终止符”-字符末尾的值为零。它很有用,因为您需要知道文本何时结束num始终是字符数+1 当你写这篇文章时: c

您需要知道,
std::string
不是内置类型。它是一个类,实现所有类型的自定义行为,例如在对象复制时创建硬拷贝

"some text" // this is a string literal, meaning the type is const char[num]
当您输入字符串文字时,它很可能位于名为“.rodata”(只读数据)的代码段内。不能合法修改此字符的值。文本还有一个“空终止符”-字符末尾的值为零。它很有用,因为您需要知道文本何时结束<由于空终止符,code>num始终是
字符数
+1

当你写这篇文章时:

const char* text = "hello world!";
// note that this is illegal:
// char* text = "hello world!"; // literal is read-only.
你只是说:

text
指向文本所在的内存

复制文本实际上需要更多的工作。必须明确做到:

char* notReadOnly = new char[30]; // you can allocate some more
// program will still interpret character of value 0 as the end, even if buffer is bigger
std::strcpy(notReadOnly, "hello world");
// use string literal as src in std::strcpy
请注意,您也需要手动删除它:

delete[] notReadOnly;
std::string
让它变得简单多了。当你写这样的东西时,它会自动复制文本:

std::string text = "some string literal";
class string
{
    char *buffer;
    std::size_t numberOfCharacters;
};
class string
{
    // rest
    string(const string &other)
    {
        numberOfCharacters = other.numberOfCharacters;
        buffer = new char[numberOfCharacters];
        // copy everything
        std::strncpy(buffer, other.buffer, numberOfCharacters);
    }
};
std::string
的复制构造函数也会制作缓冲区的硬拷贝。即使
std::string
类看起来像这样:

std::string text = "some string literal";
class string
{
    char *buffer;
    std::size_t numberOfCharacters;
};
class string
{
    // rest
    string(const string &other)
    {
        numberOfCharacters = other.numberOfCharacters;
        buffer = new char[numberOfCharacters];
        // copy everything
        std::strncpy(buffer, other.buffer, numberOfCharacters);
    }
};
每次复制时,它都会执行
缓冲区的硬拷贝,如下所示:

std::string text = "some string literal";
class string
{
    char *buffer;
    std::size_t numberOfCharacters;
};
class string
{
    // rest
    string(const string &other)
    {
        numberOfCharacters = other.numberOfCharacters;
        buffer = new char[numberOfCharacters];
        // copy everything
        std::strncpy(buffer, other.buffer, numberOfCharacters);
    }
};
请注意,这只是一个简化的示例

std::string a = "some text";
std::string b = a; // copy constructor is called. It uses method implemented above

const char* x = "some text2";
const char* y = x; // copy constructor is called. Only the address is copied, there is no hard copy of the actual buffer.
当您将变量作为参数传递给函数时,也会调用复制构造函数。编译器可以在一些常见情况下对其进行优化。

changeChar()
使用
char*
指针指向内存中某个位置的
char
(函数假定
char*
实际上指向指定大小的
char[]
数组)

固定长度数组仅以其名称引用时,会衰减为指向其第一个元素的指针。因此,当函数使用
char*
指针时,不需要(也不能)使用
操作符&
mychar[]
数组传递给
changeChar()
函数

如果不想通过指针传递
mychar
,则必须通过引用传递它(否则,通过值传递它将生成数组的副本,然后函数将无法修改原始数组)。在这种情况下,编译器可以为您推断数组大小:

template<size_t size>
void changeChar(char (&buffer)[size]) {
    strcpy_s(buffer, size, "test123");
}

char mychar[] = "hello world";
changeChar(mychar);

请注意,您提供的示例中没有使用任何引用。
std::string
不是指针,而是类。通过值传递类将创建一个副本,而通过引用传递类将允许您访问原始对象。通过引用传递指针与通过值传递指针相比没有任何净收益,而在
std::string
的情况下,它可以避免创建副本你认为在做什么?好的,但是我不是在给指针“buffer”分配mystr的内存地址吗?或者是在
changeString(&mystr)中的
&
是操作员的地址(与引用无关),它返回
mystr
的地址请参见:对不起,我是新来的,所以我不太明白。。。我的代码是否额外复制了“mystr”或字符串指针“buffer”是否直接指向“mystr”?将指针复制到char时,只复制内存地址。两个指针指向同一个位置。非常感谢,我想我明白了!所以数组在被名字引用时通常会变成一个指针,或者它仅仅是字符数组,或者我完全错了吗?任何一种固定长度的数组都会变成指针,它不仅仅是
char
数组。好的,非常感谢。这让我了解了很多。