gpt4 book ai didi

c++ - 将默认字符串指针传递给函数

转载 作者:行者123 更新时间:2023-11-27 23:58:49 25 4
gpt4 key购买 nike

我的fuu.h中有一个函数的原型(prototype)。如果有,我想传递默认值

void MyFunction(
int iError = 0,
std::string * MyProblemString = ""
);

// Some attemts:

void MyFunction(
int iError = 0,
std::string * MyProblemString = std::string{""}; // does not work to, complains about missing '*'
);

void MyFunction(
int iError = 0,
std::string * MyProblemString = &std::string{""}; // does not work to, refering address to temporary value
);

void MyFunction(
int iError = 0,
std::string * MyProblemString = *std::string{""}; // does not work to, overloaded operator not known
);

void MyFunction(
int iError = 0,
std::string * MyProblemString = ""; // could not convert ‘""’ from ‘const char [1]’ to ‘std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}’
);

in my c-file is written:

void MyFunction(
int iError,
std::string * MyProblemString,)
{
// do some stuff
}

有了 int,它工作正常。我怎样才能用字符串做同样的事情?

有一些其他构造的示例,但传递指针不起作用。 const reference default-value

谢谢

最佳答案

我认为您不了解指针的作用。我会尽力提供帮助。

Pointers

int number;      // An int
int * pointer; // A valid pointer to an int
int* pointer; // Also a valid pointer to an int
int *pointer; // Also a valid pointer to an int

“number”是一个整数类型的命名变量,具有足够大的内存块来存储分配给它的 int。这 block 内存被赋予了一个内存地址。

指针基本上就像一个 int,除了它们存储的数字是另一个变量的内存地址 - 在您的例子中是“数字”。

& 运算符将为您提供您使用它的变量的内存地址。

&number 

这将为您提供 int“数字”的内存地址。

pointer = &number;  // Pointer now contains the memory address of "number"

现在,如果您尝试像使用 int 一样使用指针,它会为您提供“数字”的地址,而不是其内容。要访问您的指针指向的任何内容,请在其前面加上 *

    void main()
{
int number = 56;

int* pointer = number; // INVALID: Pointer now pointing at memory location "56"
int* pointer = &number; // Valid: Pointer now pointing at memory location of number

int* pointer; // DANGEROUS, DO NOT LEAVE HANGING POINTERS -- leads to memory access violations

int *pointer = nullptr; // Safely initialise unused pointer
int size = 32;
pointer = new int; // Allocates a block of memory the size of 1 int. Pointer now points at this memory
pointer = new int[size]; // Allocates an array of 32 ints to pointer. Pointer now points to this block of memory
pointer = new int[8]; // Allocates another array to pointer. Pointer now points at the new array, old array has nothing pointing at it and is lost in memory!
// This is a memory leak. AVOID MEMORY LEAKS AT ALL COSTS, they can produce some of the hardest-to-find bugs you'll come across

delete pointer; // Deletes chunk of memory the size of 1 int at the memory pointer is pointing at
delete[] pointer; // Deletes array of memory following the location pointer is pointing at

// IMPORTANT: ALWAYS use delete with new, delete[] with new[]
// IMPORTANT: NEVER use new or new[] without its delete counterpart, or you will encounter memory leaks

// USEFUL:
if (pointer != nullptr)
{
// Do stuff
}
// Doing this will ensure you don't act upon memory you aren't supposed to mess with


// Print these to console to gain a better understanding.
std::cout << number << std::endl;
std::cout << &number << std::endl;
std::cout << pointer << std::endl;
std::cout << *pointer << std::endl;

system("pause");
}

注意输出:

Output on my machine (the address will vary when you run it):

56           // number
0035F7E0 // &number
0035F7E0 // pointer
56 // *pointer

Note that "&number" and "pointer" print the same thing

您可以使用 &* 将指针分配给任何东西,包括指向其他​​指针的指针,指向其他指针的指针,指向其他指针的指针,指向您想要的任何东西。像这样的事情越深入,它就会变得越困惑,但关键是(双关语意想不到的)指针是 C++ 中使用的最通用和最方便(但也很危险——内存泄漏、访问冲突、啊哈)的东西之一。

希望这对您有所帮助,如果您不明白或者这不能帮助您理解您的问题,请回复此答案。

关于c++ - 将默认字符串指针传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40668692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com