overloading

Is the *only* purpose of a *function signature* (as opp. to type) to define duplicates in a potential overload set - or are there other purposes?

落爺英雄遲暮 提交于 2019-12-01 17:37:18
Related to Why does casting a function to a function type that is identical except for return type fail? , I would like to understand, in a fuller way, the distinction between a function's type and a function's signature . For example, the type of a function must typically be considered when dealing with function pointers, and the type of the function includes the return type of that function. However, as noted in Mike Seymour's answer to the above-linked question, the signature of a function is different from the type of a function. The signature is certainly used to disambiguate from among

How can I distinguish overloads of templates with non-type parameters?

二次信任 提交于 2019-12-01 17:13:43
问题 Here are two template functions, that differ only in their template parameters. The rest of the parameters are exactly the same. template<int module> void template_const(int &a,int & b){ a = a & module; b = b % module; } template<bool x> void template_const(int &a,int & b){ int w; if (x){ w = 123; } else w = 512; a = a & w; b = b % w; } When I try to call them like this template_const<true>(a,b) or template_const<123>(a,b) the compiler tells me that the call is ambiguous. How can I call these

Advantages of Constructor Overloading

为君一笑 提交于 2019-12-01 16:56:54
问题 I am very new to Java and trying to learn the subject, having previous programming exposure in only HTML/CSS. I have started with Herbert Schildt and progressed through a few pages. I am unable to understand the exact advantages of Constructor Overloading. Isn't it easier to Overload Methods using single constructor for flexibility? Moreover if I am trying to use constructor overloading to use one object to initialize another, there are simpler ways to do it! So what are the benefits and in

How does Java method dispatch work with Generics and abstract classes?

徘徊边缘 提交于 2019-12-01 16:42:37
I ran into a situation today where Java was not invoking the method I expected -- Here is the minimal test case: (I'm sorry this seems contrived -- the 'real world' scenario is substantially more complex, and makes much more sense from a "why the hell would you do that ?" standpoint.) I'm specifically interested in why this happens, I don't care about redesign suggestions. I have a feeling this is in Java Puzzlers, but I don't have my copy handy. See the specific question in commends within Test<T>.getValue() below: public class Ol2 { public static void main(String[] args) { Test<Integer> t =

How do I use parameter overloading or optional parameters in rust?

混江龙づ霸主 提交于 2019-12-01 16:39:35
I am trying to write a print function for a binary tree and here is what I have so far: impl TreeNode { fn print(&self) { self.print(0); } fn print(&self, level: u8) { for _i in range(0,level) { print!("\t"); } match self.data { Some(x) => println!("{}",x), None => () }; match self.left { Some(ref x) => x.print(level+1), None => () }; match self.right { Some(ref x) => x.print(level+1), None => () }; } } I am getting the error: duplicate definition of value print . So I was wondering if there is a way to create functions with the same name but different arguments. Alternatively optional

In java, Can we override a method by passing subclass of the parameter used in super class method?

99封情书 提交于 2019-12-01 16:17:12
As per the rule, while overriding a method in subclass, parameters cannot be changed and have to be the same as in the super class. What if we pass subclass of parameter while overriding method ? Will it be called as overloading or overriding? Based on my query I have written some code below. I was expecting the output as "Dog eats Flesh Food" but to my surprise the output is "Animal eats Flesh Food" Will appreciate if someone can explain how does Animal method gets called when the object assigned is of type Dog ? class Food { public String toString(){ return "Normal Food"; } } class Flesh

How does Java method dispatch work with Generics and abstract classes?

﹥>﹥吖頭↗ 提交于 2019-12-01 15:40:39
问题 I ran into a situation today where Java was not invoking the method I expected -- Here is the minimal test case: (I'm sorry this seems contrived -- the 'real world' scenario is substantially more complex, and makes much more sense from a "why the hell would you do that ?" standpoint.) I'm specifically interested in why this happens, I don't care about redesign suggestions. I have a feeling this is in Java Puzzlers, but I don't have my copy handy. See the specific question in commends within

How to select a specific .m function when two exist?

邮差的信 提交于 2019-12-01 15:39:52
问题 First, here the way i'm calling the function : eval([functionName '(''stringArg'')']); % functionName = 'someStringForTheFunctionName' Now, I have two functionName functions in my path, one that take the stringArg and another one that takes something else. I'm getting some errors because right now the first one it finds is the function that doesn't take the stringArg . Considering the way i'm calling the functionName function, how is it possible to call the correct function? Edit: I tried the

Alternatives to function overloading in C

拜拜、爱过 提交于 2019-12-01 14:31:47
I'm looking for an elegant way to avoid re-writing a function, whose implementation is almost the same, but only the signature (the number of input parameters and their data types) is different. I know function overloading is not possible in C. I also know about the existence of variadic functions. But I think they won't be helpful in this situation. Consider the following problem, where we need to calculate the area of a triangle. We have two functions implementing two different formulae: S = 1/2bh and S = sqrt(s(s-a)(s-b)(s-c)). Apart from calculating the area each of the functions also

Behavior of method overloading in java [duplicate]

痞子三分冷 提交于 2019-12-01 13:57:07
This question already has an answer here: Two methods with the same name in java 3 answers I tried the following code public class HelloWorld { public void printData(Test t) { System.out.println("Reached 1"); } public void printData(newTest t) { System.out.println("Reached 2"); } public void printData(newTest1 t) { System.out.println("Reached 3"); } public static void main(String args[]) { Test t1 = new Test(); HelloWorld h = new HelloWorld(); h.printData(t1); NewTest t2 = new NewTest(); h.printData(t2); NewTest1 t3 = new NewTest1(); h.printData(t3); Test t4 = new NewTest(); h.printData(t4);