overloading

Scala: arrays and type erasure

北城以北 提交于 2020-02-27 17:29:01
问题 I'd like to write overloaded functions as follows: case class A[T](t: T) def f[T](t: T) = println("normal type") def f[T](a: A[T]) = println("A type") And the result is as I expected: f(5) => normal type f(A(5)) => A type So far so good. But the problem is the same thing doesn't work for Arrays: def f[T](t: T) = println("normal type") def f[T](a: Array[T]) = println("Array type") Now the compiler complains: double definition: method f:[T](t: Array[T])Unit and method f:[T](t: T)Unit at line 14

Implicit conversions with std::function [duplicate]

佐手、 提交于 2020-02-26 09:44:07
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Why can't my C++ compiler deduce template argument for boost function? Isn't the template argument (the signature) of std::function part of its type? I have the following: #include <functional> void Foo( std::function<void()> ); void Foo( std::function<void(int)> ); void Bar(); int main() { Foo( Bar ); // Error: ambiguous Foo( [](){} ); // Error: ambiguous Foo( std::function<void()>( Bar ) ); // Ok Foo( std:

overloading operators issue with addition

邮差的信 提交于 2020-02-26 04:16:27
问题 Having issues with adding objects. It seems to work when adding two, ex: 2.34 + 34.57 = 36.81, but fails when adding 3 or more ex: 6.78 + 9.81 + 4.59 = 79.59 <-- the total seems to increase drastically for some reason. The converter functions can be ignored since they simply translate the numerical total to english format and are working. The median and sorting, and > functions work as well, so the issue might be somewhere in the ostream, istream, or + functions. I used cout a whole bunch of

overloading operators issue with addition

耗尽温柔 提交于 2020-02-26 04:16:05
问题 Having issues with adding objects. It seems to work when adding two, ex: 2.34 + 34.57 = 36.81, but fails when adding 3 or more ex: 6.78 + 9.81 + 4.59 = 79.59 <-- the total seems to increase drastically for some reason. The converter functions can be ignored since they simply translate the numerical total to english format and are working. The median and sorting, and > functions work as well, so the issue might be somewhere in the ostream, istream, or + functions. I used cout a whole bunch of

variables declaration with same name C++

二次信任 提交于 2020-02-10 19:40:39
问题 Is this allowed? Class A{ ... ... }; A a; //Global object int main() { A a; // Local object . . . . return 0; } Here a global object has been declared after the class definition, but also a local variable has been declared. Is it ok? Why? 回答1: It's perfectly legal to "hide" the declaration of an object, with another declaration in tighter scope. Within your main function, a will refer to the local variable. Outside the main function, a will refer to the global variable. As to whether it's "ok

variables declaration with same name C++

放肆的年华 提交于 2020-02-10 19:34:21
问题 Is this allowed? Class A{ ... ... }; A a; //Global object int main() { A a; // Local object . . . . return 0; } Here a global object has been declared after the class definition, but also a local variable has been declared. Is it ok? Why? 回答1: It's perfectly legal to "hide" the declaration of an object, with another declaration in tighter scope. Within your main function, a will refer to the local variable. Outside the main function, a will refer to the global variable. As to whether it's "ok

Assignment Operator Overloading Java

我们两清 提交于 2020-02-05 07:08:25
问题 Im having trouble figuring out how to implement the equivalent of overloading the assignment operator in C++ to Java. I know there is no such thing, but I need to simulate it. I've tried overriding the Clone() function, but no luck. Any ideas? Below is my main Queue p = new Queue(); Queue q = new Queue(); p.enqueue('a'); p.enqueue(9); p.enqueue(10); p.enqueue(310); p.enqueue(8); q = p; System.out.print(p); And here is the clone function public void Clone(Queue other) throws Throwable { System

Overload dictionary subscript two times and forward call

喜夏-厌秋 提交于 2020-02-03 15:13:26
问题 I'm trying to extend Dictionary and allow extracting values casted to a certain types and with a given default value. For this I added two overloads for the subscript function, one with a default value, one without: extension Dictionary { subscript<T>(_ key: Key, as type: T.Type, defaultValue: T?) -> T? { // the actual function is more complex than this :) return nil } subscript<T>(_ key: Key, as type: T.Type) -> T? { // the following line errors out: // Extraneous argument label

Overload dictionary subscript two times and forward call

僤鯓⒐⒋嵵緔 提交于 2020-02-03 15:12:28
问题 I'm trying to extend Dictionary and allow extracting values casted to a certain types and with a given default value. For this I added two overloads for the subscript function, one with a default value, one without: extension Dictionary { subscript<T>(_ key: Key, as type: T.Type, defaultValue: T?) -> T? { // the actual function is more complex than this :) return nil } subscript<T>(_ key: Key, as type: T.Type) -> T? { // the following line errors out: // Extraneous argument label

Behavior of method overloading in java [duplicate]

徘徊边缘 提交于 2020-01-30 03:27:15
问题 This question already has answers here : Two methods with the same name in java (3 answers) Closed 6 years ago . 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