假设这是一个 ServiceContract
[ServiceContract]
public interface MyService
{
[OperationContract]
int Sum(int x, int y);
[OperationContract]
int Sum(double x, double y);
}
Run Code Online (Sandbox Code Playgroud)
在C#中允许方法重载,但WCF不允许您重载operation contracts
托管程序将抛出一段InvalidOperationException时间托管
我有一个方法具有以下重载:
string Call(string function, Dictionary<string, object> parameters, object body)
string Call(string function, Dictionary<string, object> parameters, JObject body)
Run Code Online (Sandbox Code Playgroud)
现在我添加了另一个重载:
string Call(string function)
{
return Call(function, null, (JObject) null);
}
Run Code Online (Sandbox Code Playgroud)
我添加了一个强制转换,JObject因此编译器知道它应该使用哪个重载.但Visual Studio告诉我演员阵容是多余的.但是如果没有演员,为什么我的电话不明确?
我有一个小程序:
#include<iostream>
using namespace std;
void f(int) { cout << "int\n"; }
void f(short) { cout << "short\n"; }
int main(void){
char c = 0;
f(c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它打印int.我觉得,如果这是因为"整体推广",为什么不short首选?
我也知道整数提升发生在表达式中(如A = B).但是我没有接到电话中的表情f(),吗?
如果这是重载解决规则有关,为什么传递char到F将导致到编译器宁愿int到short?
如果我删除f(int),那么f(c)会打电话f(short)!
总而言之,我的问题是,它是与"整数提升"还是仅仅是"超载解决规则"有关?为什么?
我对Java的varargs方法有点困惑:
public static int sum(int ...a) {
return 0;
}
public static double sum(double ...a) {
return 0.0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在sum()不传递任何参数的情况下调用时,则int调用了方法的版本.我不明白为什么; 通常编译器必须引发错误.
相反,当我尝试在sum没有任何参数的情况下调用时,以下代码段会生成编译器错误:
public static int sum(int ...a) {
return 0;
}
public static boolean sum(boolean ...a) {
return true;
}
Run Code Online (Sandbox Code Playgroud) 我在scala中写了这个,它不会编译:
class TestDoubleDef{
def foo(p:List[String]) = {}
def foo(p:List[Int]) = {}
}
Run Code Online (Sandbox Code Playgroud)
编译通知:
[error] double definition:
[error] method foo:(List[String])Unit and
[error] method foo:(List[Int])Unit at line 120
[error] have same type after erasure: (List)Unit
Run Code Online (Sandbox Code Playgroud)
我知道JVM没有对泛型的原生支持,所以我理解这个错误.
我可以写包装List[String],List[Int]但我很懒:)
我很怀疑,但是,有没有另一种方式表达List[String]不是同一种类型List[Int]?
谢谢.
main()存在2个有效版本C++:
int main() // version 1
int main(int argc, char **argv) // version 2
Run Code Online (Sandbox Code Playgroud)
但两种重载都不能同时共存.为什么不?(潜在用例:从终端运行程序时,如果没有传递参数,则调用第一个版本,否则第二个版本被调用.)
编译器是否执行特殊检查以允许每个二进制文件只有一个版本?
例如,Java自己String.format()支持可变数量的参数.
String.format("Hello %s! ABC %d!", "World", 123);
//=> Hello World! ABC 123!
Run Code Online (Sandbox Code Playgroud)
如何创建自己接受可变数量参数的函数?
后续问题:
我真的想为此做一个方便的捷径:
System.out.println( String.format("...", a, b, c) );
Run Code Online (Sandbox Code Playgroud)
所以我可以把它称为不那么冗长的东西:
print("...", a, b, c);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
在下面的代码中,我定义了一个简单的log函数.在main我尽量不称呼它; 我打电话std::log.然而,我自己log被称为; 我看到"日志!" 在屏幕上.有谁知道为什么?我使用G ++ 4.7和clang ++ 3.2.
#include <iostream>
#include <cmath>
double log(const double x) { std::cout << "log!\n"; return x; }
int main(int argc, char *argv[])
{
std::log(3.14);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 有没有办法在.NET(2.0)中使用反射调用重载方法.我有一个动态实例化从公共基类派生的类的应用程序.出于兼容性目的,此基类包含2个同名方法,一个包含参数,另一个不包含.我需要通过Invoke方法调用无参数方法.现在,我得到的只是一个错误告诉我,我正试图调用一个模棱两可的方法.
是的,我可以将对象转换为我的基类的实例并调用我需要的方法.最终会发生,但现在,内部并发症将无法实现.
任何帮助都会很棒!谢谢.
clang在编译以下代码时发出警告:
struct Base
{
virtual void * get(char* e);
// virtual void * get(char* e, int index);
};
struct Derived: public Base {
virtual void * get(char* e, int index);
};
Run Code Online (Sandbox Code Playgroud)
警告是:
warning: 'Derived::get' hides overloaded virtual function [-Woverloaded-virtual]
Run Code Online (Sandbox Code Playgroud)
(当然需要启用上述警告).
我不明白为什么.请注意,在Base中取消注释相同的声明会关闭警告.我的理解是,由于两个get()函数具有不同的签名,因此不能隐藏.
clang对吗?为什么?
请注意,这是在MacOS X上,运行最新版本的Xcode.
clang --version
Apple LLVM version 5.0 (clang-500.1.74) (based on LLVM 3.3svn)
Run Code Online (Sandbox Code Playgroud)
更新:与Xcode 4.6.3相同的行为.
overloading ×10
c++ ×4
.net ×2
c# ×2
java ×2
methods ×2
.net-2.0 ×1
casting ×1
compilation ×1
function ×1
hidden ×1
invoke ×1
namespaces ×1
reflection ×1
scala ×1
type-erasure ×1
typeclass ×1
virtual ×1
warnings ×1
wcf ×1