overloading

Method overloading in java regarding arguments with int/long and String/object

好久不见. 提交于 2019-12-29 02:09:24
问题 For the following program why the methods with int and String arguments are getting called instead of long and Object? Wanted to know why the compiler is choosing int over long and String over Object arguments. Note: This was asked in an interview. public class MethodOverloadingTest { public static void add(int n, int m){ System.out.println("Int method"); System.out.println(n+m); } public static void add(long n, long m){ System.out.println("Long method"); System.out.println(n+m); } public

Why can't functions be overloaded by return type? [duplicate]

孤者浪人 提交于 2019-12-29 01:35:07
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Function overloading by return type? Puzzle: Overload a C++ function according to the return value Because I have a library which exposes a bunch of functions in the form of: bool GetVal(); double GetVal(); int GetVal(); long GetVal(); //So on. And now I have to wrap these. I'd rather not rewrite the same set of functions again. I'd like to do something like template<class T> T GetVal(){} But I can't seem to

Why overload the varargs method of() in Java Stream interface?

可紊 提交于 2019-12-29 00:08:16
问题 The Stream interface has two overloads for the method of() . One of these is a variable-arity method while the other takes a single argument. Is the single-argument method a performance optimization versus passing one argument to the variable-arity method? If so, how does it improve performance? The same questions could be asked of the empty() method, which would seem to be syntax sugar around the variable-arity of() . I see that the implementation differs between these methods, with the

What's the exact semantics of deleted member functions in C++11?

强颜欢笑 提交于 2019-12-28 09:04:27
问题 struct A { A(); A(const A&); A& operator =(const A&); A(A&&) = delete; A& operator =(A&&) = delete; }; struct B { B(); B(const B&); B& operator =(const B&); }; int main() { A a; a = A(); // error C2280 B b; b = B(); // OK } My compiler is VC++ 2013 RC. error C2280: 'A &A::operator =(A &&)' : attempting to reference a deleted function I just wonder why the compiler doesn't try A& operator =(const A&); when A& operator =(A&&) is deleted? Is this behavior defined by the C++ standard? 回答1: a = A(

Polymorphism in Overloaded and Overridden Methods

假如想象 提交于 2019-12-28 07:01:49
问题 Let's take this simple Java code: public class Animal { public void eat() { System.out.println("Generic Animal Eating Generically"); } } public class Horse extends Animal { public void eat() { System.out.println("Horse eating hay "); } public void eat(String s) { System.out.println("Horse eating " + s); } } I'm trying to figure out which version of the three eat() methods will run. Now, when I type Animal a = new Animal(); a.eat(); The output is "Generic Animal Eating Generically", which is

Wrap overloaded function via std::function

╄→尐↘猪︶ㄣ 提交于 2019-12-28 04:04:21
问题 I have an overloaded function which I want to pass along wrapped in a std::function. GCC4.6 does not find a "matching function". While I did find some questions here the answers are not as clear as I would like them. Could someone tell me why the following code can not deduct the correct overload and how to (elegantly) work around it? int test(const std::string&) { return 0; } int test(const std::string*) { return 0; } int main() { std::function<int(const std::string&)> func = test; return

Why does the most negative int value cause an error about ambiguous function overloads?

纵饮孤独 提交于 2019-12-28 01:55:10
问题 I'm learning about function overloading in C++ and came across this: void display(int a) { cout << "int" << endl; } void display(unsigned a) { cout << "unsigned" << endl; } int main() { int i = -2147483648; cout << i << endl; //will display -2147483648 display(-2147483648); } From what I understood, any value given in the int range (in my case int is 4 byte) will call display(int) and any value outside this range will be ambiguous (since the compiler cannot decide which function to call). It

Function Overloading Based on Value vs. Const Reference

泄露秘密 提交于 2019-12-27 17:40:52
问题 Does declaring something like the following void foo(int x) { std::cout << "foo(int)" << std::endl; } void foo(const int &x) { std::cout << "foo(const int &)" << std::endl; } ever make sense? How would the caller be able to differentiate between them? I've tried foo(9); // Compiler complains ambiguous call. int x = 9; foo(x); // Also ambiguous. const int &y = x; foo(y); // Also ambiguous. 回答1: The intent seems to be to differenciate between invocations with temporaries (i.e. 9 ) and 'regular'

Overloading in Java and multiple dispatch

我们两清 提交于 2019-12-27 17:39:23
问题 I have a collection (or list or array list) in which I want to put both String values and double values. I decided to make it a collection of objects and using overloading ond polymorphism, but I did something wrong. I run a little test: public class OOP { void prova(Object o){ System.out.println("object"); } void prova(Integer i){ System.out.println("integer"); } void prova(String s){ System.out.println("string"); } void test(){ Object o = new String(" "); this.prova(o); // Prints 'object'!!

Overload handling of std::endl?

微笑、不失礼 提交于 2019-12-27 12:17:25
问题 I want to define a class MyStream so that: MyStream myStream; myStream << 1 << 2 << 3 << std::endl << 5 << 6 << std::endl << 7 << 8 << std::endl; gives output [blah]123 [blah]56 [blah]78 Basically, I want a "[blah]" inserted at the front, then inserted after every non terminating std::endl ? The difficulty here is NOT the logic management, but detecting and overloading the handling of std::endl . Is there an elegant way to do this? Thanks! EDIT: I don't need advice on logic management. I need