c++ - C++ 中的默认引用参数

标签 c++ class reference

我有类mizer:

class mizer {
  ...
  public:
    ...
    void getDeviation( vector<double>&, vector<int>& )
...
};

实现:

void mizer::getDeviation( vector<double>& best_mass, vector<int>& best_comb ){
   ...
}

但有时我不想提供第二个参数best_comb。所以我想设置默认值或其他东西:

void mizer::getDeviation( ..., vector<int>& best_comb = default )

我试过:

static vector<int> def();
...
void mizer::getDeviation( ..., vector<int>& best_comb = def )

但它不起作用:

/minimizer/mizer.C:69:69: error: 
non-const lvalue reference to   type 'vector<int>'  cannot bind to a value of unrelated type 'vector<int> ()'double mizer::getDeviation( vector<double>&  best_mass, vector<int>& best_comb=def ){

如何设置默认的vector引用变量?

最佳答案

static vector<int> def();声明一个静态函数 def返回 vector<int> .使用 static vector<int> def{};static vector<int> def;相反

关于c++ - C++ 中的默认引用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42955039/

相关文章:

c++ - 准备好的语句可以跨线程共享吗?

python - Tkinter 文本输入的返回值,关闭 GUI

c# - Visual Studio 2012 C# - 导入 DLL

c++ - 在 FTP 服务器中实现 "TYPE A"

c++ - 如何避免由于遍历大量数据而导致应用程序死锁?

c++ - bitset 是否保证连续性?

C++ 将属性转换为指针以减小类的大小是否有缺点?

c# - 遍历包含变量的类

php - php 中数组引用的问题

c++ - 什么时候使用引用成员?