这是我的代码示例:
class X
{
public:
void f() {}
};
class Y : public X
{
public:
X& operator->() { return *this; }
void f() {}
};
int main()
{
Y t;
t.operator->().f(); // OK
t->f(); // error C2819: type 'X' does not have an overloaded member 'operator ->'
// error C2232: '->Y::f' : left operand has 'class' type, use '.'
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器试图将operator->的责任从Y移到X?当我实现X :: op->然后我不能返回X那里 - 编译错误说"无限递归",而从X :: op->返回一些Z再次说Z没有operator->,因此更高和等级越高.
谁能解释这个有趣的行为?:)
我有以下代码:
public class Pair< T, U > {
public T first;
public U second;
}
public class Test {
public int method( Pair< Integer, Integer > pair ) {
return 0;
}
public double method( Pair< Double, Double > pair ) {
return 1.0;
}
}
Run Code Online (Sandbox Code Playgroud)
这实际上编译和工作就像人们期望的那样.但是如果返回类型相同,则不会编译,预期的"名称冲突:方法(对)和方法(对)具有相同的擦除"
鉴于返回类型不是方法签名的一部分,这种重载怎么可能?
假设我有这两个ctors:
public SomeClass(string a, Color? c = null, Font d = null)
{
// ...
}
public SomeClass(string a, Font c = null, Color? d = null)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
〜我这样做:
SomeClass sc = new SomeClass("Lorem ipsum");
Run Code Online (Sandbox Code Playgroud)
我会得到这个:"错误1以下方法或属性之间的调用是不明确的[...]"
对我来说似乎很明显,我所指的是哪一个并不重要,因为最终结果是相同的(至少在这个特殊情况下,对我而言,这就是现在最重要的事情),那么我有什么选择可以绕过这个?
编辑1:@oltman:简化示例.
我只想写
[...] new SomeClass("Lorem", Color.Green)
Run Code Online (Sandbox Code Playgroud)
代替
[...] new SomeClass("Lorem", null, Color.Green)
Run Code Online (Sandbox Code Playgroud) 我正在使用方法重载Assembly A:
public static int GetPersonId(EntityDataContext context, string name)
{
var id = from ... in context... where ... select ...;
return id.First();
}
public static int GetPersonId(SqlConnection connection, string name)
{
using (var context = new EntityDataContext(connection, false))
{
return GetPersonId(context, name);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试从第二次重载调用时Assembly B,VS会产生以下编译时错误:
"System.Data.Entity.DbContext"类型在未引用的程序集中定义.您必须添加对程序集'EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = ...'的引用.
Assembly B参考Assembly A.实体框架仅在引用Assembly A是Assembly B不使用它.来自的呼叫Assembly B如下:
using (SqlConnection connection = new SqlConnection(connectionString))
{ …Run Code Online (Sandbox Code Playgroud) 我今天遇到了这个,我很惊讶我之前没有注意到它.给出一个简单的C#程序,类似于以下内容:
public class Program
{
public static void Main(string[] args)
{
Method(); // Called the method with no arguments.
Method("a string"); // Called the method with a string.
Console.ReadLine();
}
public static void Method()
{
Console.WriteLine("Called the method with no arguments.");
}
public static void Method(string aString = "a string")
{
Console.WriteLine("Called the method with a string.");
}
}
Run Code Online (Sandbox Code Playgroud)
您将获得每个方法调用的注释中显示的输出.
我理解为什么编译器会选择它所做的重载,但为什么首先允许这样做呢?我不是在问什么是重载决策规则,我理解这些规则,但我想知道是否存在技术上的原因,为什么编译器允许基本上具有相同签名的两个重载?
据我所知,一个带有签名的函数重载只是通过另外一个可选参数与另一个重载不同,只提供了如果只需要参数(和所有前面的参数).
它所做的一件事是让程序员(可能没有给予足够的重视)认为他们正在调用与他们实际存在的不同的重载.
我想这是一个相当罕见的情况下,以及为什么这是允许的可能只是因为它根本不值得的复杂性,以禁止它的答案,但有另一个原因,C#允许函数重载从其他人的不同仅通过有一个附加的可选争论?
我知道标题没有多大意义,但代码将解释我的问题。
template<typename T>
void foo(T...) {std::cout << 'A';}
template<typename... Ts>
void foo(Ts...) {std::cout << 'B';}
int main(){
foo(1);
foo(1,2);
}
Run Code Online (Sandbox Code Playgroud)
在阅读续篇之前尝试猜测这个程序的输出:
所以输出是 AB
谁能解释为什么 1 个参数函数优先考虑省略号,而 2 个参数优先考虑可变参数模板?
R支持函数重载吗?
我想做的事情是:
g <- function(X,Y) { # do something and return something }
g <- function(X) { # do something and return something}
Run Code Online (Sandbox Code Playgroud) 好的,我知道这是不可能的,但这是制定问题标题的最佳方式.问题是,我正在尝试使用我自己的自定义类而不是浮点数(用于确定性模拟),我希望语法尽可能接近.所以,我当然希望能够写出类似的东西
FixedPoint myNumber = 0.5f;
Run Code Online (Sandbox Code Playgroud)
可能吗?
以下代码:
class Tools {
static int roll(int min, int max) {
// IMPLEMENTATION
}
static int roll(List<int> pair) {
// IMPLEMENTATION
}
}
Run Code Online (Sandbox Code Playgroud)
The name 'roll' is already defined在第二个roll函数上呈现错误.
怎么会?由于函数的参数是不同的,不应该应用多态?
编辑.更正标题以更好地反映主题.
所以,我写了这样的东西
#include <iostream>
using namespace std;
void f(int32_t i)
{
cout << "int32: " << i << endl;
}
void f(int16_t i)
{
cout << "int16: " << i << endl;
}
void f(int8_t i)
{
cout << "int8: " << i << endl;
}
void f(uint32_t i)
{
cout << "uint32: " << i << endl;
}
void f(uint16_t i)
{
cout << "uint16: " << i << endl;
}
int main() {
uint8_t i = 0u;
f(i);
return …Run Code Online (Sandbox Code Playgroud)