我假设没有,但我只是想检查 - 在C++中是否有任何方法可以执行以下操作?显然,当我尝试下面的内容时,我会得到一个关于bar的基于范围的错误.
void foo(Bar bar, int test = bar.testInt) { ... }
Run Code Online (Sandbox Code Playgroud) 我想知道这是否可以在Delphi中使用(或者如果有一个干净的方法):
type
TSomething = record
X, Y : Integer;
end;
Run Code Online (Sandbox Code Playgroud)
GetSomething( x, y ) - >返回带有这些值的记录.
...然后你有这个函数TSomething作为参数,你想默认它
function Foo( Something : TSomething = GetSomething( 1, 3 );
Run Code Online (Sandbox Code Playgroud)
编译器在这里发出错误,但我不确定是否有办法绕过它!
可以这样做吗?
它尝试过类似的东西,但是没有用.有没有办法获得类似的效果?
class A
{
public:
int foo();
void bar(int b = foo());
};
Run Code Online (Sandbox Code Playgroud) 是否有默认参数的解决方法?在C++中我会用
int foo(int k, bool check = false)
Run Code Online (Sandbox Code Playgroud)
一个繁琐的解决方法是重载一个函数.一个更容易?(没有办法只添加变量并检查函数的调用!!)
谢谢,孙
是否可以在C#中为PageSize设置默认参数的值?例如:
public virtual void Render(string reportTitle, Rectangle pageSize = PageSize.A4)
{
foreach (Page p in pages)
{
p.Render(document);
document.NewPage();
document.AddCreationDate();
document.AddTitle(reportTitle);
document.SetPageSize(pageSize);
}
}
Run Code Online (Sandbox Code Playgroud)
我在Visual Studio 2010中有以下错误:
'pageSize'的默认参数值必须是编译时常量.
c# asp.net-mvc visual-studio-2010 itextsharp default-parameters
我无法理解默认参数如何与命名函数中的多个子句交互.归结起来,为什么以下片段有效?
defmodule Lists do
def sum([], total \\ 0), do: total
def sum([h|t], total), do: h + sum(t, total)
end
Run Code Online (Sandbox Code Playgroud)
根据我的理解,编译器将其扩展为:
defmodule Lists do
def sum([]), do: sum([], 0)
def sum([], total), do: total
def sum([h|t], total), do: h + sum(t, total)
end
Run Code Online (Sandbox Code Playgroud)
所以我希望发生以下情况:
iex(1)> Lists.sum [1,2,3,4]
** (FunctionClauseError) no function clause matching in Lists.sum/1
Run Code Online (Sandbox Code Playgroud)
相反,它有效:
iex(1)> Lists.sum [1,2,3,4]
10
Run Code Online (Sandbox Code Playgroud)
使用Elixir 0.12.4.
我知道可以这样做:
int foo(int a=0, int b=1){return a+b}
Run Code Online (Sandbox Code Playgroud)
然后在没有默认参数的情况下使用它,例如:
foo(2,3)
Run Code Online (Sandbox Code Playgroud)
或者使用最后一个作为默认值,例如:
foo(2) // a=2 and b=1 default
Run Code Online (Sandbox Code Playgroud)
但我的问题是:是否可以使用第一个参数(a)的默认值并给出第二个参数的值(b)
我的第一个想法是这样做:
foo(,2) // a=0 default and b=2
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
是否存在这种语法或者这是不可能的?
function myfunc( value1, value2='defaultValue' ) {
//some stuff
}
Run Code Online (Sandbox Code Playgroud)
如果参数传入或不传入函数,我怎么知道呢?我知道如果你没有传递任何东西作为参数,将设置默认值.我实际上想要检查用户是否作为第二个参数传递任何东西(即使它与默认值相同)并更改我的返回相应的价值.
谢谢,
我有一个子类,它覆盖了基类中的方法.基类的方法有默认参数.我的子类需要在重写的方法中显示这些默认参数,尽管它们不需要是可选的.
public class BaseClass
{
protected virtual void MyMethod(int parameter = 1)
{
Console.WriteLine(parameter);
}
}
public class SubClass : BaseClass
{
//Compiler error on MyMethod, saying that no suitable method is found to override
protected override void MyMethod()
{
base.MyMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将方法签名更改为
protected override void MyMethod(int parameter = 1)
Run Code Online (Sandbox Code Playgroud)
甚至
protected override void MyMethod(int parameter)
Run Code Online (Sandbox Code Playgroud)
那很开心 我希望它接受无参数方法签名,然后在base.MyMethod()调用时允许它使用默认参数.
为什么子类的方法需要参数?
c# inheritance overriding compiler-errors default-parameters
考虑以下代码:
#include <functional>
template <typename T,typename COMP>
bool foo(T a,T b,COMP c = std::less<T>()) {
return c(a,b);
}
bool bar(int a, int b){ return a<b;}
int main(){
foo(1,2,bar); // OK
foo(1,2,std::less<int>()); // OK
foo(1,2); // error
}
Run Code Online (Sandbox Code Playgroud)
前两个调用很好,但似乎禁止让编译器COMP从默认参数中推断出类型:
<source>:14:5: error: no matching function for call to 'foo'
foo(1,2);
^~~
<source>:4:6: note: candidate template ignored: couldn't infer template argument 'COMP'
bool foo(T a,T b,COMP c = std::less<T>()) {
^
1 error generated.
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?我真的不明白为什么编译器"无法推断模板参数'COMP'",我宁愿怀疑它不允许这样做.
是否可以从默认参数推断出模板参数?如果没有,为什么?
c++ ×4
c# ×3
parameters ×2
asp.net-mvc ×1
c++98 ×1
delphi ×1
ecmascript-6 ×1
elixir ×1
inheritance ×1
itextsharp ×1
javascript ×1
overriding ×1
templates ×1