overloading

how to improve this method using polymorphism+overloading so as to reduce IS (type check)?

旧城冷巷雨未停 提交于 2019-12-17 19:51:33
问题 For example BaseClass MyBase() { public int Add(BaseClass next) { if (this is InheritedA && next is InheritedA) return 1; else if (this is InheritedA && next is InheritedB) return 2; else if (this is InheritedB && next is InheritedA) return 3; else if (this is InheritedB && next is InheritedB) return 4; } } where InheritedA , and InheritedB are its inherited classes. In fact, there are more Inherited classes, and the Add returns different results based on the order and types of its operand. I

overloading method priority in java

不羁岁月 提交于 2019-12-17 18:59:41
问题 I know that this problem discussed many times but I don't understand anyway. Research this code: public class Main { public static void var(Integer x, int y) { System.out.println("Integer int"); } public static void var(int... x) { System.out.println("int... x"); } public static void var(Integer... x) { System.out.println("Integer..."); } public static void main(String... args) { byte i = 0; Integer i2 = 127; var(i, i2); } } In my brain following rule: widening boxing boxing+varargs According

Default values on arguments in C functions and function overloading in C

梦想的初衷 提交于 2019-12-17 18:43:09
问题 Converting a C++ lib to ANSI C and it seems like though ANSI C doesn't support default values for function variables or am I mistaken? What I want is something like int funcName(int foo, bar* = NULL); Also, is function overloading possible in ANSI C? Would need const char* foo_property(foo_t* /* this */, int /* property_number*/); const char* foo_property(foo_t* /* this */, const char* /* key */, int /* iter */); Could of course just name them differently but being used to C++ I kinda used to

In C++ how is function overloading typically implemented?

核能气质少年 提交于 2019-12-17 18:28:55
问题 If there is no function overloading, the function name serves as the address of the function code, and when a function is being called, its address is easy to find using its name. However with function overloading, how exactly can the program find the correct function address? Is there a hidden table similar to virtual tables that stores the overloaded functions with their address? Thanks a lot! 回答1: The compiler can look at the call, and match that against the known existing overloaded

Best practices regarding equals: to overload or not to overload?

纵然是瞬间 提交于 2019-12-17 18:26:36
问题 Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[] args) { class Thing { final int x; Thing(int x) { this.x = x; } public int hashCode() { return x; } public boolean equals(Thing other) { return this.x == other.x; } } List<Thing> myThings = Arrays.asList(new Thing(42)); System.out.println(myThings.contains(new Thing(42))); // prints "false" } } Note that contains returns false !!! We seems to have lost our things!! The bug, of

How to use Reflection to Invoke an Overloaded Method in .NET

纵饮孤独 提交于 2019-12-17 17:42:38
问题 Is there a way to Invoke an overloaded method using reflection in .NET (2.0). I have an application that dynamically instantiates classes that have been derived from a common base class. For compatibility purposes, this base class contains 2 methods of the same name, one with parameters, and one without. I need to call the parameterless method via the Invoke method. Right now, all I get is an error telling me that I'm trying to call an ambiguous method. Yes, I could just cast the object as an

The relationship of overload and method return type in Java?

[亡魂溺海] 提交于 2019-12-17 16:49:42
问题 If there are two methods, they have different parameters, and their return types are different . Like this: int test(int p) { System.out.println("version one"); return p; } boolean test(boolean p, int q) { System.out.println("version two"); return p; } If the return types are same, of course this is overload . But since the return types are different , can we regard this as overload still? 回答1: consider following points for overloading: First and important rule to overload a method in java is

Java overloading vs overriding

纵然是瞬间 提交于 2019-12-17 15:44:44
问题 Hi I just want to make sure I have these concepts right. Overloading in java means that you can have a constructor or a method with different number of arguments or different data types. i.e public void setValue(){ this.value = 0; } public void setValue(int v){ this.value = v; } How about this method? Would it still be considered overloading since it's returning a different data type? public int setValue(){ return this.value; } Second question is: what is overriding in java? Does it relate to

c++ overloaded virtual function warning by clang?

半腔热情 提交于 2019-12-17 15:35:26
问题 clang emits a warning when compiling the following code: 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); }; The warning is: warning: 'Derived::get' hides overloaded virtual function [-Woverloaded-virtual] (the said warning needs to be enabled of course). I don't understand why. Note that uncommenting the same declaration in Base shuts the warning up. My understanding is that since

Overriding == operator. How to compare to null? [duplicate]

烂漫一生 提交于 2019-12-17 15:14:38
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I check for nulls in an ‘==’ operator overload without infinite recursion? There is probably an easy answer to this...but it seems to be eluding me. Here is a simplified example: public class Person { public string SocialSecurityNumber; public string FirstName; public string LastName; } Let's say that for this particular application, it is valid to say that if the social security numbers match, and both