overloading

C# Method Overload Problem With Class Derived From Generic Abstract Class

心不动则不痛 提交于 2019-12-04 05:40:56
I am working on a project, and I have a generic abstract type that takes a type parameter that is itself derived from the abstract type. If you want to know why I would do this, please see this question . I have run into an interesting problem with overloading a method in a derived class that is defined in the abstract class. Here is a code sample: public abstract class AbstractConverter<T, U> where U : AbstractConvertible where T : AbstractConverter<T, U> { public abstract T Convert(U convertible); } public class DerivedConvertibleConverter : AbstractConverter<DerivedConvertibleConverter,

Cannot deduce template argument that is a function

大兔子大兔子 提交于 2019-12-04 05:34:07
Why cannot F be deduced for proxy() ? It should be possible because I am restricting it - only for functions that return an int . #include <utility> #include <iostream> #include <type_traits> using namespace std; int foo(int bar) { cout << "int" << endl; return 2; } float foo(float bar) { cout << "float" << endl; return 1; } template <typename F, typename... Args> typename enable_if< is_same< typename result_of<F(Args...)>::type, int >::value, typename result_of<F(Args...)>::type >::type proxy(F func, Args&&... args) { return func(forward<Args>(args)...); } int main() { proxy(foo, 5); } Here

Is there a way to overload a function based on different Result type in Delphi?

我只是一个虾纸丫 提交于 2019-12-04 05:28:17
Function overloading by return type? has a very detailed answer on the rational on function overloading by return type, and from what I can see Delphi does not allow this, but are there any workarounds to overload a function based on different return type in Delphi? The implicit and explicit conversion operators for records permit overloading by return type: namely, the type being converted to: type TFoo = record class operator Implicit(const AFoo: TFoo): Integer; class operator Implicit(const AFoo: TFoo): string; end; Depending on the context, using a value of type TFoo will call the

How do I verify the number of invocations of overloaded method using Mockito?

做~自己de王妃 提交于 2019-12-04 05:02:44
问题 How do I check if bar(Alpha, Baz) called bar(Xray, Baz) using Mockito - without actually calling the later, given my MCVE class Foo : public class Foo { public String bar(Xray xray, Baz baz) { return "Xray"; } public String bar(Zulu zulu, Baz baz) { return "Zulu"; } public String bar(Alpha alpha, Baz baz) { if(alpha.get() instanceof Xray) { return bar((Xray)alpha.get(), baz); } else if(alpha.get() instanceof Zulu) { return bar((Zulu)alpha.get(), baz); } else { return null; } } } Currently, I

Method overloading with argument vararg of object class and array of object class

雨燕双飞 提交于 2019-12-04 04:49:09
问题 If I attempt to overload the method flexiPrint() in a class Varargdemo then it generates a compile time error. The compiler treats the following signatures the same: public static void flexiPrint(Object... data){} public static void flexiPrint(Object[] data){} Can someone explain to me why they are treated the same? I haven't been able to find the answer. 回答1: Object... is nothing but it is an array, that means same as defining Object[] ... (three dots) represents varargs in java. We usually

Polymorphism and overloading with static methods in C#.

只愿长相守 提交于 2019-12-04 03:56:20
I have been trying to generate a Factory supposed to return a different object of a common interface (say Item ) according to the input parameter (I call it a context) of the function getItem(A context) Now, assume I define a new type of context: B which inherits from A . I wanted to return a different item depending on whether the object passed to the factory was of class B or A . I tried to do as follows (overloading the method): class Factory { static Item getItem(A context) {...} static Item getItem(B context) {...} } This works fine if I do something like this: B bContext=new B(); Item it

Automatic type conversion in Java?

折月煮酒 提交于 2019-12-04 03:43:26
问题 Is there a way to do automatic implicit type conversion in Java? For example, say I have two types, 'FooSet' and 'BarSet' which both are representations of a Set. It is easy to convert between the types, such that I have written two utility methods: /** Given a BarSet, returns a FooSet */ public FooSet barTOfoo(BarSet input) { /* ... */ } /** Given a FooSet, returns a BarSet */ public BarSet fooTObar(FooSet input) { /* ... */ } Now say there's a method like this that I want to call: public

Overloading function using varargs

寵の児 提交于 2019-12-04 03:33:00
问题 This will not compile: public class Methods { public static void method(Integer... i) { System.out.print("A"); } public static void method(int... i) { System.out.print("B"); } public static void main(String args[]) { method(7); } } This will compile and work: public class Methods { public static void method(Integer i) { System.out.print("A"); } public static void method(int i) { System.out.print("B"); } public static void main(String args[]) { method(7); } } First and second example are very

TypeScript: specialized signatures for EventEmitter subclass events

我的梦境 提交于 2019-12-04 03:28:15
I have a base class EventEmitter , which has the on method to bind handlers on specific events: class EventEmitter { on(event: string, handler: Function) { /* add handler internally */ } protected emit(event: string, ...args) { /* call all handlers listening on event */ } } Now I have various subclasses, which can emit different events with different arguments. I'd like to "declare" which events can be emitted by that specific class: class MyClass extends EventEmitter { on(event: 'event1', handler: (arg1: number) => void): void; on(event: 'event2', handler: (arg1: string, arg2: number) => void

operator<< overloading ostream

江枫思渺然 提交于 2019-12-04 03:27:06
问题 In order to use cout as such : std::cout << myObject, why do I have to pass an ostream object? I thought that was an implicit parameter. ostream &operator<<(ostream &out, const myClass &o) { out << o.fname << " " << o.lname; return out; } Thanks 回答1: You aren't adding another member function to ostream , since that would require redefining the class. You can't add it to myClass , since the ostream goes first. The only thing you can do is add an overload to an independent function, which is