标签: default-arguments

用于指定其存在/不存在的默认参数的惯用方法

我经常看到python代码采用默认参数,并且在未指定时具有特殊行为.

如果我想要这样的行为:

def getwrap(dict, key, default = ??):
    if ???: # default is specified
        return dict.get(key, default)
    else:
        return dict[key]
Run Code Online (Sandbox Code Playgroud)

如果我要自己动手,我最终会得到类似的东西:

class Ham:
    __secret = object()
    def Cheese(self, key, default = __secret):
        if default is self.__secret:
            return self.dict.get(key, default)
        else:
            return self.dict[key]
Run Code Online (Sandbox Code Playgroud)

但是当我确实有一个标准时,我不想发明一些愚蠢的东西.在Python中这样做的惯用方法是什么?

python default-arguments

3
推荐指数
1
解决办法
190
查看次数

指定默认参数C++

我写了下面的课

class worker
{
   int action;
   int doJob(int type,int time = 0);
   public:
   int call();
}
Run Code Online (Sandbox Code Playgroud)

功能doJob就像

int worker::doJob(int type,int time = 0)
{
          ....code here
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,我收到以下错误

 error: the default argument for parameter 1 of 'int worker::doJob(int, int)' has not yet been parsed
Run Code Online (Sandbox Code Playgroud)

当然这是默认参数规范的问题.那么原型的问题是什么?

c++ arguments default-arguments

3
推荐指数
1
解决办法
1406
查看次数

错误:在先前的规范之后为参数指定的默认参数

对我来说非常简单的任务,我不知道为什么这会给我带来问题,我只是让两个模型类尝试在他们的方法中没有任何逻辑的情况下编译,使用已经给我的头文件和声明.老实说,这只是一个剪切和粘贴工作,但我仍然遇到了这个金色的金块 -

cbutton.cpp:11:44: error: default argument given for parameter 4 of ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.h:7:5: error: after previous specification in ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.cpp:11:44: error: default argument given for parameter 5 of ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.h:7:5: error: after previous specification in ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.cpp:19:41: error: default argument given for parameter 1 of ‘void cio::CButton::draw(int)’ [-fpermissive]
cbutton.h:11:10: error: after previous …
Run Code Online (Sandbox Code Playgroud)

c++ default-arguments

3
推荐指数
1
解决办法
1万
查看次数

如何用C++中的默认参数初始化"unsigned char*"?

我有一个带有以下签名的方法的类:

void print(unsigned char *word);
Run Code Online (Sandbox Code Playgroud)

我需要设置""为默认值word,我该怎么做?

我尝试了显而易见void print(unsigned char *word="");但我得到以下错误:

error: cannot initialize a parameter of type
  'unsigned char *' with an lvalue of type 'const char [1]'
    void print(unsigned char *word="");
Run Code Online (Sandbox Code Playgroud)

因为我不能word用字符串文字初始化我该怎么办?

c++ initialization default-arguments

3
推荐指数
2
解决办法
7302
查看次数

如何在python中重写方法默认参数?

方法默认参数可以显然被覆盖:

>>> class B:
...     def meth(self, r=True): print r
>>> class D(B):
...     def meth(self, r=False): print r
... D().meth()
False
>>> B().meth()
True
Run Code Online (Sandbox Code Playgroud)

这怎么可能 ?它被认为是不好的风格?

python methods inheritance python-2.7 default-arguments

3
推荐指数
1
解决办法
4812
查看次数

具有默认参数的虚函数,奇怪的输出

这段代码的输出是15,我真的不知道为什么.我认为它x=5foo功能中使用,但我不知道为什么.谁能帮我 ?

#include <iostream>
#include <string>

using namespace std;


struct A
{
    virtual int foo(int x = 5)
    {
        return x*2;
    }
};

struct B : public A
{
    int foo(int x = 10)
    {
        return x*3;
    }
};



int main(int argc, char** argv)
{
  A* a = new B;
  cout << a->foo();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions language-lawyer default-arguments

3
推荐指数
1
解决办法
46
查看次数

带有初始化参数的函数定义和带有较少参数的函数调用

考试期间我遇到了一个令人困惑的问题.请帮我理解这个概念.代码段包括:

void xyz(int a = 0, int b, int c = 0)
{
    cout << a << b << c;
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是以下哪些电话是非法的?

(假设h和g被声明为整数)

(a) xyz();    (b) xyz(h,h);

(c) xyz(h);    (d) xyz(g,g);
Run Code Online (Sandbox Code Playgroud)

代码:

(1)(a)和(c)是正确的 (2)(b)和(d)是正确的

(3)(a)和(b)是正确的 (4)(b)和(c)是正确的

我试图用C++编译代码,我得到了这个错误:

错误:预期';',','或')'在'='之前令牌
void xyz(int a = 0,int b = 0,int c = 0)

帮助我理解这个概念.

c++ default-arguments

3
推荐指数
1
解决办法
713
查看次数

编译器确定参数是否给出或省略

如何确定/检查在编译时是否给出或省略了函数参数?

bool a_function(char* b, size_t len=0) {

      // no run time check such as if (len ......
      // just compile time check

      // ...
}
Run Code Online (Sandbox Code Playgroud)

如何实现?

c++ function default-arguments

3
推荐指数
1
解决办法
70
查看次数

使函数参数默认为周围范围

考虑到下面提供的以下代码/方案,是否有可能省略指定 this?

有没有办法更改代码,以便函数/构造函数自动获取周围的范围,甚至可以作为模板参数?

#include <iostream>

class AttrBase{
public:
  virtual int g()const = 0;
};

class ABase{
public:
 void reg(const char* name, AttrBase* att){
  std::cout << name << ": " << att->g()<< std::endl;
 }
};


class Attr : public AttrBase{
public: 
  Attr(const char* name, int val, ABase* parent /* = this */) // something like this
   :v(val)
  { 
    parent->reg(name, this);
  };
  int g()const override{return  v;};
  int v;
};


class D:public ABase{
  Attr a{"a", 1, this};
  Attr b{"b", 2, this};
  Attr …
Run Code Online (Sandbox Code Playgroud)

c++ default-arguments

3
推荐指数
1
解决办法
72
查看次数

在泛型类之外定义函数会产生编译错误

上下文

我写了以下最小的工作示例

#include <iostream>

template <typename T>
struct A {
  enum myenum {faa, fee, fii};
  myenum m_m;
  A(const myenum& m = faa): m_m(m) {}
};

int main() {
  A<int> a1;
  A<int> a2(A<int>::fii);

  std::cout << a1.m_m << std::endl
            << a2.m_m << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译并执行它会导致

$ g++ main.cpp && ./a.out
0
2
Run Code Online (Sandbox Code Playgroud)

问题

但是,当函数的定义写在类之外时(即如下图)

#include <iostream>

template <typename T>
struct A {
  enum myenum {faa, fee, fii};
  myenum m_m;
  A(const myenum& m);
};

template <typename T>
A<T>::A(const myenum& m = …
Run Code Online (Sandbox Code Playgroud)

c++ templates default-arguments

3
推荐指数
1
解决办法
35
查看次数