overloading

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

做~自己de王妃 提交于 2019-12-04 03:14:13
问题 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

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

天涯浪子 提交于 2019-12-04 02:57:54
问题 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

How to detect ambiguous method calls that would cause a ClassCastException in Java 8?

笑着哭i 提交于 2019-12-04 02:31:55
We are currently in the process of migrating an application from Java 7 to Java 8. After fixing a some compilation issues, I stumbled upon an issue similar to the following question: ClassCast Error: Java 7 vs Java 8 . To summarise, here is a sample code that shows the issue: public class Test { public static void main(String[] args) { System.out.println(String.valueOf(getVal("xxx"))); // 7: prints the result, 8: Exception } @SuppressWarnings("unchecked") public static <T> T getVal(String param) { // do some computation based on param... return (T) result; // actual return type only depends on

Alternatives to function overloading in C

二次信任 提交于 2019-12-04 02:30:10
问题 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

Is it possible to overload the ShowDialog method for forms and return a different result?

只愿长相守 提交于 2019-12-04 01:51:29
EDIT: This method actually works great and I asked it then found the solution later. I added the correct call in the overloaded ShowDialog() method (it's not exacly an overload, or even an override, but it works just the same. My new question is the one at the bottom. I have a form in which you click one of three buttons. I have defined an enum for the returned results. I want to make the call: MyFormResults res = MyForm.ShowDialog(); I can add a new ShowDialog method with this code: public new MyFormResults ShowDialog() { //Show modal dialog base.ShowDialog(); //This works and somehow I

C++ calling completely wrong (virtual) method of an object

白昼怎懂夜的黑 提交于 2019-12-03 23:22:23
I have some C++ code (written by someone else) which appears to be calling the wrong function. Here's the situation: UTF8InputStreamFromBuffer* cstream = foo(); wstring fn = L"foo"; DocumentReader* reader; if (a_condition_true_for_some_files_false_for_others) { reader = (DocumentReader*) _new GoodDocumentReader(); } else { reader = (DocumentReader*) _new BadDocumentReader(); } // the crash happens inside the following call // when a BadDocumentReader is used doc = reader->readDocument(*cstream, fn); The files for which the condition is true are processed fine; the ones for which it is false

Reference initialization and direct vs indirect binding

空扰寡人 提交于 2019-12-03 22:30:46
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, user-defined conversions are considered ... The result of the call to the conversion function, as

'max_user_connections' set to 200 - still getting error

混江龙づ霸主 提交于 2019-12-03 20:28:04
问题 Here is the mysql error: Connect failed: User 'db2498' has exceeded the 'max_user_connections' resource (current value: 200). I set the my.cnf: [mysqld] max_connections = 500 max_user_connections = 200 I set the max_user_connections in mysql for the user to 200 also. I've had 1400 people hit the site in about 10-20 minutes. Each stay on for an average of 14 seconds, and I got about 1400 of these messages. I'm using PHP/Mysql. This is the database class: class DB{ public function __construct()

Overloading (or alternatives) in Python API design

无人久伴 提交于 2019-12-03 20:10:54
问题 I have a large existing program library that currently has a .NET binding, and I'm thinking about writing a Python binding. The existing API makes extensive use of signature-based overloading. So, I have a large collection of static functions like: Circle(p1, p2, p3) -- Creates a circle through three points Circle(p, r) -- Creates a circle with given center point and radius Circle(c1, c2, c3) -- Creates a circle tangent to three curves There are a few cases where the same inputs must be used

implicit instantiation of undefined template 'class'

与世无争的帅哥 提交于 2019-12-03 19:51:48
问题 When trying to offer functions for const and non-const template arguments in my library I came across a strange problem. The following source code is a minimal example phenomenon: #include <iostream> template<typename some_type> struct some_meta_class; template<> struct some_meta_class<int> { typedef void type; }; template<typename some_type> struct return_type { typedef typename some_meta_class< some_type >::type test; typedef void type; }; template<typename type> typename return_type<type>: