标签: overloading

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

varargs和重载的错误?

Java varargs实现中似乎存在一个错误.当方法使用不同类型的vararg参数重载时,Java无法区分适当的类型.

它给了我一个错误 The method ... is ambiguous for the type ...

请考虑以下代码:

public class Test
{
    public static void main(String[] args) throws Throwable
    {
        doit(new int[]{1, 2}); // <- no problem
        doit(new double[]{1.2, 2.2}); // <- no problem
        doit(1.2f, 2.2f); // <- no problem
        doit(1.2d, 2.2d); // <- no problem
        doit(1, 2); // <- The method doit(double[]) is ambiguous for the type Test
    }

    public static void doit(double... ds)
    {
        System.out.println("doubles");
    }

    public static void doit(int... is)
    {
        System.out.println("ints"); …
Run Code Online (Sandbox Code Playgroud)

java overloading variadic-functions

21
推荐指数
2
解决办法
3856
查看次数

按模板参数编号重载模板类

是否可以拥有相同类的多个版本,这些版本仅在模板参数的数量上有所不同?

例如:

template<typename T>
class Blah {
public:
    void operator()(T);
};

template<typename T, typename T2>
class Blah {
public:
    void operator()(T, T2);
};
Run Code Online (Sandbox Code Playgroud)

我正在尝试模拟仿函数类型的东西,它可以采用可变数量的参数(最多可写出不同模板的数量).

c++ templates overloading

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

无法解析已从C#重写和重载的F#方法

以下F#代码声明了基类和后代类.基类有一个带有默认实现的虚拟方法'Test'.后代类重写基类方法,并添加新的重载'Test'方法.此代码编译良好,并且在访问后代'Test'方法时不会出现任何问题.

F#代码:

module OverrideTest
  [<AbstractClass>]
  type Base() =
    abstract member Test : int -> int
    default this.Test x = x + 1

  type Descendant() =
    inherit Base()
    override this.Test x    = x - 1
    member this.Test (x, y) = x - y
Run Code Online (Sandbox Code Playgroud)

但是,尝试从C#调用后代的'Test'覆盖会导致编译错误:

var result = td.Test(3); < - 方法'Test'没有重载需要1个参数

完整的C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client
{
  class Program
  {
    static void Main(string[] args)
    {
      var td = new OverrideTest.Descendant();
      var result = td.Test(3);
      Console.WriteLine(result); …
Run Code Online (Sandbox Code Playgroud)

c# f# interop overriding overloading

21
推荐指数
1
解决办法
546
查看次数

Varargs在Java中的方法重载

以下代码无法编译.

package varargspkg;

public class Main {

    public static void test(int... i) {
        for (int t = 0; t < i.length; t++) {
            System.out.println(i[t]);
        }

        System.out.println("int");
    }

    public static void test(float... f) {
        for (int t = 0; t < f.length; t++) {
            System.out.println(f[t]);
        }

        System.out.println("float");
    }

    public static void main(String[] args) {
        test(1, 2);  //Compilation error here quoted as follows.
    }
}
Run Code Online (Sandbox Code Playgroud)

发出编译时错误.

对于测试的引用是不明确的,varargspkg.Main中的方法test(int ...)和varargspkg中的方法test(float ...)匹配

这似乎是显而易见的,因为在方法调用的参数值test(1, 2);可以提升int以及float

如果任何一个或两个参数后缀为Ff,则编译.


但是,如果我们使用相应的包装器类型表示方法签名中的接收参数,如下所示 …

java overloading variadic-functions java-6

21
推荐指数
1
解决办法
6357
查看次数

在C++中,命名参数的范围是否包含其默认值的表达式?

示例:这是合法的C++ 14吗?

#include <iostream>
static int d() {
    return 42;
}
static int e(int d = d()) {
    return d;
}
int main() {
    std::cout << e() << " " << e(-1) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

g ++ 5.4 -std=c++14喜欢它,但clang ++ 3.8 -std=c++14抱怨:

samename.cxx:3:23: error: called object type 'int' is not a function or function pointer
static int e(int d = d()) {return d;}
                     ~^
Run Code Online (Sandbox Code Playgroud)

c++ scope overloading default-value

21
推荐指数
2
解决办法
1319
查看次数

Delphi/pascal:使用不同的原型重载构造函数

我正在尝试创建一个子类的TForm

  1. 某些案例的特殊构造函数,以及
  2. 一个默认构造函数,它将保持与当前代码的兼容性.

这是我现在的代码:

interface
  TfrmEndoscopistSearch = class(TForm)
  public
    /// original constructor kept for compatibility
    constructor Create(AOwner : TComponent); overload; override;
    /// additional constructor allows for a caller-defined base data set
    constructor Create(AOwner : TComponent; ADataSet : TDataSet; ACaption : string = ''); overload;
  end;
Run Code Online (Sandbox Code Playgroud)

它似乎工作,但我总是得到编译器警告:

[Warning] test.pas(44): Method 'Create' hides virtual method of base type 'TCustomForm'
  • 添加"过载"; 在第二个构造函数之后将无法编译."[错误] test.pas(44):"创建"声明与之前的声明不同".
  • 使第二个构造函数的一个类函数编译时没有任何错误或警告,但在运行时死于访问冲突(所有成员变量都是零).

delphi constructor overloading

20
推荐指数
3
解决办法
3万
查看次数

Delphi:方法'Create'隐藏了基础的虚拟方法 - 但它就在那里

考虑假设的对象层次结构,从以下开始:

TFruit = class(TObject)
public
    constructor Create(Color: TColor); virtual;
end;
Run Code Online (Sandbox Code Playgroud)

及其后代:

TApple = class(TFruit)
public
    constructor Create(); overload; virtual;
    constructor Create(Color: TColor); overload; override; //deprecated. Calls other constructor - maintaining the virtual constructor chain
end;
Run Code Online (Sandbox Code Playgroud)

这里的想法是我已经覆盖了基类的构造函数,其重载也恰好是虚拟的.

德尔福抱怨:

方法'Create'隐藏基类型'TFruit'的虚方法

除了它不隐藏它 - 它就在那里!

  • 覆盖了祖先中的虚拟方法,并且
  • 我用另一个版本重载了

这是怎么回事?

delphi overloading

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

Overload resolution with ref-qualifiers

While working with ref-qualified function overloads, I'm getting different results from GCC (4.8.1) and Clang (2.9 and trunk). Consider the following code:

#include <iostream>
#include <utility>

struct foo
{
    int& bar() &
    {
        std::cout << "non-const lvalue" << std::endl;
        return _bar;
    }
    //~ int&& bar() &&
    //~ {
    //~     std::cout << "non-const rvalue" << std::endl;
    //~     return std::move(_bar);
    //~ }
    int const& bar() const &
    {
        std::cout << "const lvalue" << std::endl;
        return _bar;
    }
    int const&& bar() const …
Run Code Online (Sandbox Code Playgroud)

c++ overloading language-lawyer c++11 ref-qualifier

20
推荐指数
1
解决办法
2304
查看次数

为什么函数重载在C++中会产生模糊错误?

在下面的代码段,在函数调用f(1),1是文字型的int并在第一功能void f(double d)参数类型是double和第二函数void f(short int i)参数类型是短整型.

1是一个int不是double类型的类型,那么为什么编译器会产生模糊错误?

#include <iostream>
using namespace std;

void f(double d)  // First function
{
    cout<<d<<endl;
}

void f(short int i) // Second function
{
    cout<<i<<endl;
}

int main()
{
    f(1); // 1 is a literal of type int
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ overloading short ambiguous c++14

20
推荐指数
1
解决办法
1626
查看次数