overloading

Why can't an interface implementation return a more specific type?

谁都会走 提交于 2019-11-30 21:26:03
问题 If an interface specifies a property or method to return another interface, why is it not allowed for implementations of the first interface to "change" the return type into a more specific type? Let's take an example to illustrate: interface IFoo { IBar GetBar(); } interface IBar { } class Foo : IFoo { // This is illegal, we are not implementing IFoo properly public Bar GetBar() { return new Bar(); } } class Bar : IBar { } I know how to make it work, that's not my concern. I can just either:

Overload resolution C++ for const member functions

自古美人都是妖i 提交于 2019-11-30 21:02:53
Assume you have a class T with two member functions char foo() const {...} char foo() {...} . It is my understanding that when called for a constant T, we resolve to (1); and for a non-constant T, we resolve to (2). is that correct? which rule is invoked in this resolution? (reference to standard great, but a helpful brief summary appreciated) Notes: I tried to google for it, but old hits I got on SO were cases for other overload resolutions involving const. However, link to an old SO actually explaining the above obviously great. This came up when re-reading Stroustrup's "The C++ programming

Why does Wikipedia say “Polymorphism is not the same as method overloading or method overriding.”

a 夏天 提交于 2019-11-30 20:46:09
I have looked around and could not find any similar question. Here is the paragraph I got from Wikipedia : Polymorphism is not the same as method overloading or method overriding. Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class. Method overloading refers to methods that have the same name but different signatures inside the same class. Method overriding is where a subclass replaces the implementation of one or more of its parent's methods. Neither method overloading nor method overriding are by themselves

What is method overloading? [duplicate]

廉价感情. 提交于 2019-11-30 20:41:18
This question already has an answer here: What Does “Overloaded”/“Overload”/“Overloading” Mean? 8 answers I've found resources that say method overloading is the ability for a language to use the same method with a different outcome, depending on context. Somehow, when I read other definitions, I fell like that's not the whole definition. Is there more to method overloading? That's just a very general way of describing it. Method overloading allows you to use a single method name, but "overload it" (provide more than one version) depending on "context" (which is typically the type or number of

Kotlin: What can I do when a Java library has an overload of both primitive and boxed type?

假装没事ソ 提交于 2019-11-30 20:18:11
For example, FastUtil's IntArrayList has a push method that accepts both int (primitive) and Integer (boxed), but Kotlin sees these both as the same function push(Int) , therefore I cannot use the function at all as the function is ambiguous. What should I do when a Java library has overloads for both the primitive and boxed type? (p.s. I am aware that I can use the add(int) method. I am in search of what to do if I come across such a problem in the future.) Consider these methods in Java: void f(int x) { } void f(Integer y) { } In Kotlin, they are seen as f(x: Int) f(x: Int!) The second

Performance differences between overloading or optional parameters?

非 Y 不嫁゛ 提交于 2019-11-30 20:16:22
问题 No, not answered there ↑ I wonder if I should be using optional parameters in C#. Until now I was always overloading methods. But optional parameters are nice too, cleaner and less code. And I use them in other languages so I'm also used to them in some way. Is there anything that speaks against using them ? Performance is the first key point for me. Would it drop ? Example code: class Program { // overloading private static void test1(string text1) { Console.WriteLine(text1 + " " + "world");

How does VB.NET compiler choose which extension overload to run?

試著忘記壹切 提交于 2019-11-30 20:13:36
Got an interesting oddity - thought someone might be able to help. This came out of some fun with nullable types from this question: How to check if an object is nullable? Option Strict On Module Test ' Call this overload 1 <Extension()> Function IsNullable(obj As ValueType) As Boolean Return False End Function ' Call this overload 2 <Extension()> Function IsNullable(Of T As {Structure})(obj As Nullable(Of T)) As Boolean Return True End Function Sub Test() ' a is an integer! Dim a As Integer = 123 ' calling IsNullable as an extension method calls overload 1 and returns false Dim result1 As

Can we overload a function based on only whether a parameter is a value or a reference?

陌路散爱 提交于 2019-11-30 20:05:11
I got the answer NO! Because passing by value and passing by reference looks identical to the caller. However, the code below compiles right class A { public: void f(int i) {} void f(int& i) {} }; But when I try to use it, there is compile error. int main () { A a; int i = 9; int& j = i; a.f(1); a.f(i); a.f(j); return 0; } Why does not the compiler disable it even without knowing it is going to be used? Yes, they can be overloaded based on reference or not. That is why it's perfectly fine to have them coexist like that; they are different. The problem has to do with ambiguity. While f(1) can

Why ambiguous error when using varargs overloading with primitive type and wrapper class? [duplicate]

空扰寡人 提交于 2019-11-30 20:03:35
This question already has an answer here: Ambiguous varargs methods 4 answers I do not understand why here in case 1, it is not giving compilation error, contrary in case 2 (varargs), it gives compilation error. Can anyone please elaborate what differences the compiler makes in these two cases? I went through many posts about it, but not able to understand it yet. Case #1 public class Test { public void display(int a) { System.out.println("1"); } public void display(Integer a) { System.out.println("2"); } public static void main(String[] args) { new Test().display(0); } } The Output is: 1 Case

Function overloading in C using GCC - compiler warnings

拟墨画扇 提交于 2019-11-30 19:18:57
I am attempting to implement function overloading in C , and I am very close. I am using C99 so the _Generic keyword introduced in C11 is not available to me. I have developed some working code, but when I compile it I get a couple warnings. Working example: #include <stdio.h> #define print(x) \ __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), int ), print_int(x) , \ __builtin_choose_expr(__builtin_types_compatible_p(typeof(x), char[]), print_string(x), \ (void)0)) void print_int(int i) { printf("int: %d\n", i); } void print_string(char* s) { printf("char*: %s\n", s); } int main