标签: default-parameters

带有一个默认参数的构造函数

假设我有一堂课

class C {
       C(int a=10);
};
Run Code Online (Sandbox Code Playgroud)

为什么我打电话

C c;
Run Code Online (Sandbox Code Playgroud)

C(int =10)调用构造函数,如果我调用

C c();
Run Code Online (Sandbox Code Playgroud)

调用默认构造函数?怎么避免这个?我想只执行我的构造函数,我试图将默认构造函数设为私有,但它不起作用.

c++ constructor default-parameters

7
推荐指数
2
解决办法
2712
查看次数

代码执行派生类方法,但从基类方法获取默认参数

有人可以解释为什么下面的代码的结果将是"B类:: 1"?

为什么派生类的虚方法使用基类的默认参数而不是自己的默认参数?对我来说这很奇怪.提前致谢!

码:

#include <iostream>

using namespace std;

class A
{
public:
    virtual void func(int a = 1)
    {
        cout << "class A::" << a;
    }
};

class B : public A
{
public:
    virtual void func(int a = 2)
    {
        cout << "class B::" << a;
    }
};

int main()
{
    A * a = new B;
    a->func();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ virtual derived default-parameters

7
推荐指数
3
解决办法
900
查看次数

为什么不允许使用此默认模板参数?

我有以下课程:

template <typename Type = void>
class AlignedMemory {
public:
    AlignedMemory(size_t alignment, size_t size)
        :   memptr_(0) {
        int iret(posix_memalign((void **)&memptr_, alignment, size));
        if (iret) throw system_error("posix_memalign");
    }
    virtual ~AlignedMemory() {
        free(memptr_);
    }
    operator Type *() const { return memptr_; }
    Type *operator->() const { return memptr_; }
    //operator Type &() { return *memptr_; }
    //Type &operator[](size_t index) const;
private:
    Type *memptr_;
};
Run Code Online (Sandbox Code Playgroud)

并尝试实例化一个自动变量,如下所示:

AlignedMemory blah(512, 512);
Run Code Online (Sandbox Code Playgroud)

这会出现以下错误:

src/cpfs/entry.cpp:438:错误:'blah'之前缺少模板参数

我究竟做错了什么?是void不是允许的默认参数?

c++ templates g++ default-parameters

6
推荐指数
2
解决办法
2312
查看次数

为什么成员变量不能用作参数的默认值?

可能重复:
非静态成员作为非静态成员函数的默认参数

如果我错了,请纠正我,但我认为默认参数的工作方式如下:

当编译器看到函数调用时,它开始将参数压入堆栈.当参数耗尽时,它将开始将默认值推送到堆栈,直到填满所有必需参数(我知道这是一个简化,因为参数实际上是从右向左推送的,所以它将从默认值开始,但是想法是一样的).

如果这是真的,为什么不能将成员变量用作默认值?在我看来,由于编译器像往常一样在呼叫站点推送它们,它应该能够解决它们就好了!

编辑由于答案似乎被我的问题误解了,让我澄清一下.我知道情况就是这样,而且我知道该语言允许和不允许的内容.我的问题是为什么语言设计师选择不允许这样做,因为它似乎自然而然地起作用.

c++ language-lawyer default-parameters

6
推荐指数
1
解决办法
233
查看次数

覆盖C#中的默认参数

复制真的很简单,输出很奇怪;

预期输出为"bbb bbb"实际输出为"aaa bbb"

有没有人得到任何MSDN解释这种行为?我找不到任何东西.

((a)new b()).test();
new b().test();


public class a
{
    public virtual void test(string bob = "aaa ")
    {
        throw new NotImplementedException();
    }
}

public class b : a
{
    public override void test(string bob = "bbb ")
    {
        HttpContext.Current.Response.Write(bob);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# default-parameters

6
推荐指数
1
解决办法
209
查看次数

如何将静态方法用作策略设计模式的默认参数?

我想创建一个使用类似于此的策略设计模式的类:

class C:

    @staticmethod
    def default_concrete_strategy():
        print("default")

    @staticmethod
    def other_concrete_strategy():
        print("other")

    def __init__(self, strategy=C.default_concrete_strategy):
        self.strategy = strategy

    def execute(self):
        self.strategy()
Run Code Online (Sandbox Code Playgroud)

这给出了错误:

NameError: name 'C' is not defined
Run Code Online (Sandbox Code Playgroud)

替换strategy=C.default_concrete_strategystrategy=default_concrete_strategy将工作但是,默认情况下,策略实例变量将是静态方法对象而不是可调用方法.

TypeError: 'staticmethod' object is not callable
Run Code Online (Sandbox Code Playgroud)

如果我删除@staticmethod装饰器它会工作,但还有其他方法吗?我希望自己记录默认参数,以便其他人立即看到如何包含策略的示例.

此外,是否有更好的方法来公开策略而不是静态方法?我不认为实现完整的课程在这里有意义.

python static-methods strategy-pattern default-parameters python-3.x

6
推荐指数
1
解决办法
1371
查看次数

如何使用默认参数将函数传递给std :: thread?

所以我有gcc版本4.8.1,g ++版本4.6.4,使用标志:-std = c ++ 0x和-pthread.

我将问题简化为显示的代码并仍然得到原始错误.

我在下面编译,但当我取消注释线程"两"的两行时,我得到代码下面显示的错误消息

#include <iostream>
#include <thread>
using namespace std;

void print_int(int x=7);
void print_A(){
    cout << "A\n";
}

int main(){
    thread one (print_int,17);
    //thread two (print_int);
    thread three (print_A);

    one.join();
    //two.join();
    three.join();

    return 0;
}

void print_int(int x){
    cout << x << '\n';
}
Run Code Online (Sandbox Code Playgroud)

我试图解析错误消息,但我仍然不知道发生了什么......

    In file included from /usr/include/c++/4.6/thread:39:0,
             from def_params.cpp:2:

    /usr/include/c++/4.6/functional: In member function ‘void std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__call(std::tuple<_Args ...>&&, std::_Index_tuple<_Indexes ...>, typename std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__enable_if_void<_Res>::type) [with _Res = void, _Args = {}, …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading default-parameters c++11

6
推荐指数
1
解决办法
1402
查看次数

带有可选参数的JavaScript函数

我是来自Python背景的JavaScript的新手.在Python中,参数可以作为键和值传递:

def printinfo( name, age = 35 ):
   print "Name: ", name
   print "Age ", age
   return;
Run Code Online (Sandbox Code Playgroud)

然后可以这样调用该函数:

printinfo( age=50, name="miki" )
printinfo( name="miki" )
Run Code Online (Sandbox Code Playgroud)

这些参数可以在JavaScript函数中传递吗?

我希望能够传递一个或多个参数.例如一个JavaScript函数:

function plotChart(data, xlabel, ylabel, chart_type="l"){
    ...
} 
Run Code Online (Sandbox Code Playgroud)

我希望能够只传递数据和图表类型,标签是可选的,例如:

plotChart(data, chart_type="pie")
Run Code Online (Sandbox Code Playgroud)

这可以用JavaScript吗?

javascript named-parameters default-parameters

6
推荐指数
2
解决办法
2万
查看次数

如何在 dart 函数中将函数作为可选参数传递并带有默认值?

这就是我想要实现的:

void fun({
  bool Function(int i) predicate = (i) => false,
}) {
  // do something with 'predicate(something)'
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

可选参数的默认值必须是constant.dart(non_constant_default_value)。

我能够通过以下方法解决此错误:

bool falsePredicate(int i) => false;

void fun({
  bool Function(int i) predicate = falsePredicate,
}) {
  // do something with 'predicate(something)'
}
Run Code Online (Sandbox Code Playgroud)

但现在问题来了,为什么我不能像第一组代码那样直接创建一个默认函数值呢?第一种情况和第二种情况似乎没有什么区别。第一种方法中给出的函数为什么不是常数?

default-parameters dart

6
推荐指数
1
解决办法
1063
查看次数

根据另一个参数的值设置一个参数的默认值

所以我想创建一个函数,生成从“开始”到“结束”与“大小”一样多的连续数字。对于迭代,它将在函数内部计算。但我在设置参数“end”的默认值时遇到问题。在我进一步解释之前,先看一下代码:

# Look at this -------------------------------
#                                           ||
#                                           \/
def consecutive_generator(size=20, start=0, end=(size+start)):
    i = start
    iteration = (end-start)/size

    arr = []
    temp_size = 0

    while temp_size < size:
        arr.append(i)

        i += iteration
        temp_size += 1

    return arr

# with default end, so the 'end' parameter will be 11
c1= consecutive_generator(10, start=1)
print(c1)

# with end set
c2= consecutive_generator(10, end=20)
print(c2)


Run Code Online (Sandbox Code Playgroud)

从上面可以看出(关于'end'参数的默认值),我想要实现的是'end'参数,其默认值为'start'+'size'参数(那么迭代将是1)

输出肯定会出错。那么我该怎么做呢?(这是我第一次在 stackoverflow 上提问,如果我犯了错误,抱歉)

(关闭)

python parameters function default-parameters

6
推荐指数
1
解决办法
2114
查看次数