标签: overload-resolution

为什么Java选择这种重载超过另一种

任何人都可以向我解释为什么Java选择第二次重载而不是第一次?

public static void foo (int a, double b, double...c) {}
public static void foo (double...a) {}
public static void bar ()
{
    // this is the second
    foo(1);
}
Run Code Online (Sandbox Code Playgroud)

我认为当我1作为参数传递时,Java会选择第一个参数,因为int它更具体而不是double

谢谢

java overload-resolution

0
推荐指数
1
解决办法
41
查看次数

重载C++虚函数的分辨率 - 引用与指针

我对C++重载分辨率的行为感到困惑.我有两个班,A和B,A <:B.A具有虚函数f,B应该覆盖该函数.

但是,当我使用引用调用虚函数时,虚函数似乎不像虚函数那样工作.起初我认为这是由于对象被分配在堆栈而不是堆上,但现在我发现即使我使用对堆上分配的对象的引用也会发生这种奇怪的行为.

对此有何解释?

#include<iostream>

using namespace std;

class A{
public:
    virtual void foo(){ cout << "A::foo" << endl; }
};

class B : public A{
public:
    virtual void foo(){ cout << "B::foo" << endl; }
};

void test_pt(A* pt){
    cout << "test_pt " << (int)pt << " ";
    pt->foo();
}

void test_ref(A ref){
    cout << "test_ref " << (int)&ref << " ";
    ref.foo();
}

int main(int argc, char* argv[]){
    // pointers to objects allocated on heap
    A* heap_pt_a = …
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions overloading reference overload-resolution

0
推荐指数
1
解决办法
82
查看次数

传递NULL作为参数时,函数重载如何工作?

我知道NULL是#defined为0.它似乎是一个int转换为指针类型的常量.那么当有两个重载函数时会发生什么:一个采用指针类型,另一个采用int类型.这是如何工作的nullptr

#include <iostream>
using namespace std;

void callMe(int* p)
{
    cout << "Function 1 called" << endl;
}

void callMe(int i)
{
    cout << "Function 2 called" << endl;
}

int main()
{
    callMe(nullptr);
    callMe(NULL);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道callMe(nullptr)肯定会调用第一个函数.但是哪个函数会被调用callMe(NULL)?我在我的电脑上编译了这段代码.只有一个警告.

g++ -std=c++11 nullptr_type.cpp
nullptr_type.cpp: In function 'int main()':
nullptr_type.cpp:44:14: warning: passing NULL to non-pointer argument 1 of 'void callMe(int)' [-Wconversion-null]
   callMe(NULL);
          ^
Run Code Online (Sandbox Code Playgroud)

我的代码编译没有任何问题,当我运行它时,我看到callMe(NULL)称为函数2. Visual Studio也编译代码,它调用函数2.

但是,当我尝试在在线GeeksForGeeks IDE上编译我的代码时,我遇到了一些编译器错误.

https://ide.geeksforgeeks.org/UsLgXRgpAe

我想知道为什么会出现这些错误. …

c++ overloading overload-resolution c++11

0
推荐指数
1
解决办法
266
查看次数

std::string 隐式转换优先级,string_view 优于 const char*

我有两个重载函数:

void Set(const char * str) { std::cout << "const char * setter: " << str << "\n"; }
void Set(const std::string_view & sv) { std::cout << "string_view setter: " << sv << "\n"; }
Run Code Online (Sandbox Code Playgroud)

我在一个std::string. 它选择std::string_view重载而不是 const char * 重载,即它选择std::stringto std::string_viewover std::stringto的隐式转换const char *

这是在 中的保证行为std::string,还是他们的选择是任意的?如果这是一个有保证的行为,他们是如何使它更喜欢 string_view 转换而不是另一个?

c++ stl overload-resolution implicit-conversion c++17

0
推荐指数
1
解决办法
89
查看次数

c#:Func <T>参数中的重载决策

写这个功能:

static TResult reduce<TSource, TResult>(ParallelQuery<TSource> source,
                                        Func<TResult> seedFactory,
                                        Func<TResult, TSource, TResult> aggregator) {
    return source.Aggregate(seedFactory, aggregator, aggregator, x => x);
}                
Run Code Online (Sandbox Code Playgroud)

但我得到一个编译错误:

误差为1方法的类型参数"System.Linq.ParallelEnumerable.Aggregate( ,System.Linq.ParallelQuery<TSource>, TAccumulate,, System.Func<TAccumulate,TSource,TAccumulate> )"不能从使用推断.尝试显式指定类型参数.System.Func<TAccumulate,TAccumulate,TAccumulate>System.Func<TAccumulate,TResult>

我想要使​​用的重载是这一个, 而编译器似乎认为它也可以是这个.

我该怎么帮忙呢?

c# generics overloading overload-resolution

-1
推荐指数
1
解决办法
139
查看次数

为什么使用const的方法而不是没有const的第一个方法

void print( int & a ){ cout << a << endl; }
void print( const int & a ){ cout << 2*a << endl; }
void print( float a ){ cout << 3*a << endl; }
Run Code Online (Sandbox Code Playgroud)

为什么2输出print(1)

c++ methods const function overload-resolution

-3
推荐指数
1
解决办法
82
查看次数