overloading

What does that have to do with function overloading?

人走茶凉 提交于 2019-12-05 04:20:46
This is basically a copy from the example given in Item 21. Overriding Virtual Functions in Herb Sutter's book Exceptional C++ . #include <iostream> #include <complex> using namespace std; class Base { public: virtual void f(int); virtual void f(double); virtual ~Base() {}; }; void Base::f(int) { cout << "Base::f(int)" << endl; } void Base::f( double ) { cout << "Base::f(double)" << endl; } class Derived: public Base { public: void f(complex<double>); }; void Derived::f(complex<double>) { cout << "Derived::f(complex)" << endl; } int main() { Base* pb = new Derived; pb->f(1.0); delete pb; } The

Java, Static Method Binding and Generics all rolled up with some Method Overloading

匆匆过客 提交于 2019-12-05 03:29:11
So as the title implies my question is a bit odd and complicated. I know what I'm about to do breaks all the rules of "good" programming practices but hey, what's life if we don't live a little? So what I did was create the following program. (Note this was part of a larger experiment to really try and understand generics so some of the function names maybe a bit out of order) import java.util.*; public class GenericTestsClean { public static void test2() { BigCage<Animal> animalCage=new BigCage<Animal>(); BigCage<Dog> dogCage=new BigCage<Dog>(); dogCage.add(new Dog()); animalCage.add(new Cat(

<?= operator C++ greater less question mark equals sign

空扰寡人 提交于 2019-12-05 02:58:00
I saw <?= and >?= used in a code: http://community.topcoder.com/stat?c=problem_solution&rm=151152&rd=5854&pm=2923&cr=310333 I tried to compile without the includes to test if it's standard, but it didn't work. I then added the includes, but it still gives the same error: question-mark.cpp:15:5: error: expected primary-expression before ‘?’ token question-mark.cpp:15:6: error: expected primary-expression before ‘=’ token question-mark.cpp:15:9: error: expected ‘:’ before ‘;’ token question-mark.cpp:15:9: error: expected primary-expression before ‘;’ token #include <stdio.h> #include <algorithm>

Overloading a super class's function

淺唱寂寞╮ 提交于 2019-12-05 02:47:12
Is there something in the C++ standard that prevents me from overloading a super class's function? Starting with this pair of classes: class A { // super class int x; public: void foo (int y) {x = y;} // original definition }; class B : public A { // derived class int x2; public: void foo (int y, int z) {x2 = y + z;} // overloaded }; I can call B::foo() easily: B b; b.foo (1, 2); // [1] But if I try to call A::foo() ... B b; b.foo (12); // [2] ... I get a compiler error: test.cpp: In function 'void bar()': test.cpp:18: error: no matching function for call to 'B::foo(int)' test.cpp:12: note:

Why is Scala's behavior in case of overloading with by-name parameters different from the case with by-value parameters?

柔情痞子 提交于 2019-12-05 02:33:34
Given this Scala code: object test { def byval(a: Int) = println("Int") def byval(a: Long) = println("Long") def byname(a: => Int) = println("=> Int") def byname(a: => Long) = println("=> Long") def main(args: Array[String]) { byval(5) byname(5) } } the call byval(5) compiles correctly, but byname fails to compile: ambiguous reference to overloaded definition Why? I would expect to observe the same behavior for by-value and by-name parameters with respect to overloading… How can it be fixed? That's because JVM does not support a "by-name" parameter, so Scala has to implement it in another way.

Method overloading and generics

爱⌒轻易说出口 提交于 2019-12-05 02:24:41
Java typically prefers normal methods to generic ones when choosing which overloaded method is correct, which could generate the following sscce : public class GenericsTest { public static void main(String[] args) { myMethod(Integer.class, 10); myMethod(String.class, "overloaded method"); } public static <T> void myMethod(Class<T> klass, T foo) { System.out.println("hello world"); } public static <T> void myMethod(Class<T> klass, String bar) { System.out.println(bar); } } Output: hello world overloaded method Is there any way to force Java to use the Generic version? No, not short of deleting

Generics and calling overloaded method from difference class - precedence issue [duplicate]

帅比萌擦擦* 提交于 2019-12-05 02:07:55
This question already has an answer here: Passing generic parameter results in wrong overload being called 1 answer First of all, sorry for the title, but I couldn't think about anything better ... My problem can be presented by simple code sample: public static class Test<T> { public static int GetInt(T source) { return Convert.ToInt32(source); } } public static class Convert { public static int ToInt32(byte source) { return 30; } public static int ToInt32(object source) { return 10; } } Why does Console.WriteLine(Test<byte>.GetInt(20)); prints 10 , instead of 30 ? I always thought that

Java Compile Error: Method reference in combination with overloading

匆匆过客 提交于 2019-12-05 02:06:31
I have the following class with an overloaded method: import java.util.ArrayList; import java.util.concurrent.Callable; public abstract class Test { public void test1 () { doStuff (ArrayList::new); // compilation error } public void test2 () { doStuff ( () -> new ArrayList<> ()); } public abstract void doStuff (Runnable runable); public abstract void doStuff (Callable<ArrayList<String>> callable); } The method test1 results in a compilation error with the error message The method doStuff(Runnable) is ambiguous for the type Test . I've added a third method test3 which looks like this: public

Polymorphism - Overloading/Overriding

断了今生、忘了曾经 提交于 2019-12-05 02:05:38
问题 I know that this question has been done to death at StackOverflow and that there are numerous questions posted on this already. I've probably read every one of them and yet, there's this niggling doubt : I think I understand Overloading pretty well, and Overriding. What gets me is Polymorphism. For example, the accepted answer to this question explains this with shape.Draw() . I'm confused as to how this is different from Overriding (other times I'm confused with how it is different from

Overload resolution with extern “C” linkage

血红的双手。 提交于 2019-12-05 01:15:12
In a mixed C/C++ project, we need to make a call from C to a C++ function. The function to be called is overloaded as three separate functions, but we can ignore that from the C-side, we just pick the one most suitable and stick to that one. There's two ways to do this: (1) write a small C++ wrapper with a extern "C" function that forwards the call to the chosen overloaded function, or (2) the hackish way to just declare the one function we want to call from C as extern "C". The question is, is there any disadvantages (apart from nightmares and bad karma) to go for the second variant? In other