overloading

Reference initialization and direct vs indirect binding

元气小坏坏 提交于 2020-01-01 07:19:12
问题 Consider the following case struct A { operator int(); }; int &&x = A(); The spec says at http://eel.is/c++draft/dcl.init.ref#5 about whether the reference binding is direct or indirect In all cases except the last (i.e., creating and initializing a temporary from the initializer expression), the reference is said to bind directly to the initializer expression. The case above doesn't match the last, but the second last bullet. If T1 or T2 is a class type and T1 is not reference-related to T2,

Overloading c++ typecasting (functions)

蹲街弑〆低调 提交于 2020-01-01 04:59:05
问题 Using C++ style typecastings (all 4) look exactly like some function template . e.g. template<typename TO, typename FROM> TO dynamic_cast (FROM p); will be used as, dynamic_cast<Derived*>(p); // p is Base* Why is it not allowed to overload them by language standard for custom usage ? (like we can overload the keywords like new/delete or other operators ) 回答1: Why is it not allowed to overload them by language standard for custom usage? I suppose that's because the standard committee, when

Why overloaded methods have lower priority than instance method

落爺英雄遲暮 提交于 2020-01-01 02:45:31
问题 I have base class A public class A { public virtual void Method(A parameter) { Console.WriteLine(MethodBase.GetCurrentMethod()); } public virtual void Method(B parameter) { Console.WriteLine(MethodBase.GetCurrentMethod()); } } Inhereted B public class B : A { public virtual void Method(object parameter) { Console.WriteLine(MethodBase.GetCurrentMethod()); } public override void Method(A parameter) { Console.WriteLine(MethodBase.GetCurrentMethod()); } public override void Method(B parameter) {

Which method is called? (Integer… a) vs. (int a, int b)

眉间皱痕 提交于 2020-01-01 01:14:10
问题 I just found out about a very interesting Java trick: void method1(Integer... a){ } So you can give this method as many integers as you want. Now if I have a similar (overloaded) method like this: void method1(int a, int b){ } Which method runs when I execute the following line: method1(1, 2); Well, I could find that out very easily by just testing it out with different method instructions but when I think about the "rules" in "overloading" methods then I have to make sure that every

Can I overload operators for builtin classes in Python?

早过忘川 提交于 2019-12-31 05:31:28
问题 Is it possible to overload an operator for a builtin class in Python 3? Specifically, I'd like to overload the + / += (i.e: __add__ operator for the str class, so that I can do things such as "This is a " + class(bla) . 回答1: You can't change str 's __add__ , but you can define how to add your class to strings. I don't recommend it, though. class MyClass(object): ... def __add__(self, other): if isinstance(other, str): return str(self) + other ... def __radd__(self, other): if isinstance(other

Java Method Overloading with Boxing/Widening

ε祈祈猫儿з 提交于 2019-12-31 04:54:12
问题 I am working on Java Se 7 OCA and could not figure out why below code does not compile. aMethod call in main method gives compile error stating ambiguous method. Precedence rules between widening and boxing seems to clash in this overloading method sample. public class Overloading { public static void main(String[] args) { Byte i = 5; byte k = 5; aMethod(i, k); } static void aMethod(byte i, Byte k) { System.out.println("Inside 1"); } static void aMethod(byte i, int k) { System.out.println(

Calling overloaded functions with “null” reference

丶灬走出姿态 提交于 2019-12-31 03:32:30
问题 Let us say I have following overloaded functions public class Test { public static void funOne(String s){ System.out.print("String function"); } public static void funOne(Object o){ System.out.print("Object function"); } public static void main(String[] args) { funOne(null); } } Why would funOne(null) call the method with String argument signature? what is the precedence for overloading here? 回答1: The class that is lower in the class hierarchy will have precedence in this case. In other words

Extension method resolution with nullable value type params

喜你入骨 提交于 2019-12-31 00:03:29
问题 public static class Extension { public static void Test(this DateTime? dt) { } } void Main() { var now = DateTime.Now; Extension.Test(now); // ok now.Test(); // compile time error } I'm just curious, why is the compiler not able to resolve the same method when called as an extension? 回答1: A DateTime is not convertible to Nullable<DateTime> explicitly. The C# specification, 7.6.5.2 Extension method invocations: An extension method is eligible if: Mj is accessible and applicable when applied to

Java 7 overloading with varargs [duplicate]

心已入冬 提交于 2019-12-30 18:45:11
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: bug with varargs and overloading? could anyone explain me how this one works: class Vararg { static void vararg(int... x) { System.out.println("Integer..."); } static void vararg(long... x) { System.out.println("long..."); } public static void main(String [] args) { int s = 0; vararg(s,s); } } Get compile time error class Vararg { static void vararg(Integer... x) { System.out.println("Integer..."); } static void

Can we overload main() function in C++? [duplicate]

喜欢而已 提交于 2019-12-30 16:40:08
问题 This question already has answers here : Is main() overloaded in C++? (6 answers) Closed 2 years ago . Since C+++ allows function overloading, can we overload main() ? For example, int main(const std::string &) { return 0; } int main(int argc, char *argv[]) { return main("calling overloaded main"); } gcc-4.3.4 doesn't compile this, and gives these errors: (see at ideone) prog.cpp:4: error: first argument of ‘int main(const std::string&)’ should be ‘int’ prog.cpp:4: error: ‘int main(const std: