Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我想使用非静态对象变量作为默认方法参数_C++ - Fatal编程技术网

C++ 我想使用非静态对象变量作为默认方法参数

C++ 我想使用非静态对象变量作为默认方法参数,c++,C++,可能重复: 也因为“这可能不用于此上下文”而失败我该怎么办 代码的上下文也有一些问题。请忽略它们。参数的默认值必须是静态已知的。不能将运行时值用作默认参数 后者失败了,因为它不是有效的语法,没有任何意义。参数的默认值必须静态已知。不能将运行时值用作默认参数 后者失败,因为它不是有效的语法,没有任何意义。使用重载: LinkedInteger accessElement(int index){ return accessElement(index, &DataArray[0]);

可能重复:

也因为“这可能不用于此上下文”而失败我该怎么办


代码的上下文也有一些问题。请忽略它们。

参数的默认值必须是静态已知的。不能将运行时值用作默认参数


后者失败了,因为它不是有效的语法,没有任何意义。

参数的默认值必须静态已知。不能将运行时值用作默认参数

后者失败,因为它不是有效的语法,没有任何意义。

使用重载:

LinkedInteger accessElement(int index){
  return accessElement(index, &DataArray[0]);
}
使用重载:

LinkedInteger accessElement(int index){
  return accessElement(index, &DataArray[0]);
}
尝试:

尝试:


您可能会使用单例模式。您可能会使用单例模式。这是错误的>>“参数的默认值必须是静态已知的”。@Nawaz,嗯,看来您是对的;嗯,我想它至少应该是一个全局的。它也可以是一个函数调用。确实可以;但这是因为C++允许你从函数调用初始化全局变量。错误的是:“参数的默认值必须静态已知”。纳瓦兹,看来你是对的。嗯,我想它至少应该是一个全局的。它也可以是一个函数调用。确实可以;但这是因为C++允许您从函数调用初始化全局变量。我更喜欢重载答案。但这确实显示了一种方法,可以绕过关于什么可以和什么不能用作默认参数的初始值设定项的特定限制。我更喜欢重载答案。但这确实显示了一种方法,可以绕过关于哪些可以和哪些不能用作默认参数的初始值设定项的特定限制。
LinkedInteger accessElement(int index){
  return accessElement(index, &DataArray[0]);
}
// 0 or NULL; your preference

LinkedInteger accessElement(int index, LinkedInteger *startElement = 0)
{
   if (startElement == 0)
       startElement = &DataArray[0];  // or just startElement = DataArray;
   // ...
}