我在 Sybase 中声明了一个存储过程,其中一个参数是 datetime 类型。现在我想为这个日期时间分配一个默认值。
这是声明:
create procedure Procedure
(
@fromDate datetime = getdate()
)
...
Run Code Online (Sandbox Code Playgroud)
但是 Sybase 给了我一个错误
Number (102) Severity (15) State (1) Server (SERVER) Procedure (Procedure) Incorrect syntax near '('.
Run Code Online (Sandbox Code Playgroud)
是否有可能做到这一点?如果没有,是否有解决方法?
我写了一篇关于C++编程的考试.有一个问题我和我的教授不同意.问题是,以下功能是否有效:
#include <iostream>
using namespace std;
void f(int=4, long=10, double=3.14);
int main( int argc , char ** argv )
{
f( , ,8);
return EXIT_SUCCESS;
}
void f(int i, long l, double d) {
cout << i << " " << " " << l << " " << d;
}
Run Code Online (Sandbox Code Playgroud)
我说它不会起作用,但我的教授说它肯定会起作用,因为函数声明中的默认参数.我用MSVC尝试过它并没有用.这是编译器特定的吗?我如何说服我的教授在任何编译器中都不起作用,在考试中提高我的分数?
我有以下将文件转换为 Base64 的函数。如果未输入文件路径,如何使该函数接受文件路径的默认值?
B64 -f $文件路径
function B64{
param (
[Parameter (Mandatory = $True, ValueFromPipeline = $True)]
[Alias("file")]
$f
)
$File = "\converted.txt"
$FilePath = ([Environment]::GetFolderPath("Desktop")+$File)
$Content = Get-Content -Path $f
$converted = [convert]::ToBase64String([System.Text.encoding]::Unicode.GetBytes($Content))
$numChar = $converted.length
$incriment = 275
$pre = "STRING powershell -enc "
$string = "STRING "
function splitLines{
While ($converted)
{
$x,$converted = ([char[]]$converted).where({$_},'Split',$incriment)
$x -join ''
}
}
Run Code Online (Sandbox Code Playgroud) 当我构建项目时,VC#表示不允许使用Default参数说明符.它引导我到这个代码:
public class TwitterResponse
{
private readonly RestResponseBase _response;
private readonly Exception _exception;
internal TwitterResponse(RestResponseBase response, Exception exception = null)
{
_exception = exception;
_response = response;
}
Run Code Online (Sandbox Code Playgroud)
可能是我的错误?
为什么C#编译器不会对具有默认参数的方法感到困惑?
在下面的代码中SayHello()可以参考:
但是这段代码编译成功没有任何模糊错误.
class Program
{
private static void SayHello()
{
Console.WriteLine("Hello 1");
return;
}
private static void SayHello(string arg1 = null)
{
Console.WriteLine("Hello 2");
return;
}
private static void SayHello(string arg1 = null, string arg2 = null)
{
Console.WriteLine("Hello 3");
return;
}
private static void SayHello(string arg1 = null, string arg2 = null, string arg3 = null)
{ …Run Code Online (Sandbox Code Playgroud) 我的代码看起来像这样
static void SomeVoid(object obj1, object ojb2 = someDefaultValue) {
// Do Something Here
}
Run Code Online (Sandbox Code Playgroud)
编译器说'obj2'的默认参数值必须是编译时常量。我能做什么 ?string.Empty我是someDefaultValue 。
构造函数:
A()
{
std::cout<<"In A const";
}
A(int a = 3)
{
std::cout<<"In a with default :"<<a;
}
Run Code Online (Sandbox Code Playgroud)
创建对象:
A a;
A a1(4);
Run Code Online (Sandbox Code Playgroud)
上面的代码显示错误:重载'A()'的调用是不明确的
是否有一种简洁的方法来组合默认函数参数值和选项,因此当在函数调用中指定参数时,参数采用提供的默认值,但它的值是否为零?
例如:
class MyObj {
var foobar:String
init(foo: String?="hello") {
self.foobar = foo!
}
}
let myObj = MyObj() // standard use of default values - don't supply a value at all
println(myObj.foobar) // prints "hello" as expected when parameter value is not supplied
var jimbob: String? // defaults to nil
...
// supply a value, but it is nil
let myObj2 = MyObj(foo: jimbob) // <<< this crashes with EXC_BAD_INSTRUCTION due to forced unwrap of nil value
println(myObj2.foobar)
Run Code Online (Sandbox Code Playgroud)
...或者是给成员常量/变量默认值的最佳选择,然后只有在为构造函数提供值时才更改它们,如下所示: …
我刚刚注意到,此函数(使用默认参数)不会在编译时引起错误。
function buildAddress(address1 = 'N/A', address2: string) {
displayAddress( address1 +' '+ address2);
}
Run Code Online (Sandbox Code Playgroud)
但是此功能(使用可选参数)可以。
function buildAddress(address1?: string, address2: string) {
displayAddress( address1 +' '+ address2);
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?
我真的对这种行为感到惊讶,这很正常吗?有什么好处吗?这是功能还是错误?
compiler-errors function optional-parameters default-parameters typescript
void g(int n, decltype(n) = 0); // ok
void f(int n, int = n); // error : default argument references parameter 'n'
int main()
{
f(1); // want it to be same as f(1, 1);
}
Run Code Online (Sandbox Code Playgroud)
为什么C++不允许参数成为默认参数?
理由是什么?
c# ×3
c++ ×3
c++11 ×2
function ×2
.net-3.5 ×1
ambiguous ×1
constructor ×1
object ×1
powershell ×1
standards ×1
swift ×1
sybase ×1
typescript ×1