我的fuu.h中有一个函数的原型。如果有
,我想传递默认值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
THX
答案 0 :(得分:2)
我认为你不明白指针是做什么的。我会尽力帮忙。
指针
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。这块内存被赋予一个内存地址。
指针基本上就像一个int,除了它们存储的数字另一个变量的内存地址 - 在你的情况下,“数字”。
&
运算符将为您提供使用它的变量的内存地址。
&number
这将为您提供int“number”的内存地址。
pointer = &number; // Pointer now contains the memory address of "number"
现在,如果您尝试使用类似int的指针,它将为您提供“number”的地址,而不是其内容。要访问指针所指向的内容,请在其前面添加*
。
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");
}
注意输出:
我的机器上的输出(运行时地址会有所不同):
56 // number
0035F7E0 // &number
0035F7E0 // pointer
56 // *pointer
请注意,“&amp; number”和“指针”打印相同的内容
您可以使用&
和*
指定任何指针,包括指向指向其他指针的其他指针的其他指针的指针,指向您想要的任何内容。你用这样的东西变得越来越麻烦,但重点是(双关语)指针是C ++中使用最通用和最方便(但也很危险 - MEMORY LEAKS,ACCESS VIOLATIONS,AHHH)的东西之一。 / p>
希望这有帮助,如果您不理解这个答案的回复,或者这不能帮助您理解您的问题。
答案 1 :(得分:1)
您遇到的问题是因为您无法在不处理指针应指向的内存的情况下为指针指定默认值。如果您使用std::string
而不是指向std::string
的指针,那么您可以使用MyFunction()
中的简单字符串文字语法。
如果您需要将指针传递给字符串,因为您计划操纵字符串或用于其他目的,您可以为字符串指定默认值NULL
。由于NULL
没有指向任何内存,因此这是一个非常理智的指针默认值。然后,您可以检查此值,并在该特殊情况下分配新的std::string
。这就是我在MyFunction2
中所做的。
HOWEVER 如果分配内存,则必须将内存释放到某处。在我的例子中,只有在我创建它的情况下,才释放MyFunction2
内的内存。如果提供了值,我不会释放该值,而是将其留给调用函数。如何管理内存将取决于您的使用案例,但不要忘记这一关键步骤。
#import <iostream>
void MyFunction(
int iError = 1,
std::string MyProblemString = std::string(""))
{
std::cout << "\tiError = " << iError << ", and MyProblemString = " << MyProblemString << std::endl;
}
void MyFunction2(
int iError = 1,
std::string * MyProblemString = NULL)
{
bool shouldFree = MyProblemString == NULL;
if (shouldFree){
MyProblemString = new std::string("");
}
std::cout << "\tiError = " << iError << ", and MyProblemString = " << *MyProblemString << std::endl;
if(shouldFree){
delete MyProblemString;
}
}
int main(){
std::string * test = new std::string("test");
std::cout << "Testing with MyFunction:" << std::endl;
std::cout << "\tTest default arguments:" << std::endl;
MyFunction();
std::cout << "\tTest only first argument:" << std::endl;
MyFunction(0);
std::cout << "\tTest both arguments" << std::endl;
MyFunction(2, *test);
std::cout << std::endl;
std::cout << "Testing with MyFunction2:" << std::endl;
std::cout << "\tTest default arguments:" << std::endl;
MyFunction2();
std::cout << "\tTest only first argument:" << std::endl;
MyFunction2(0);
std::cout << "\tTest both arguments" << std::endl;
MyFunction2(2, test);
delete test;
}