overloading

Why overload true and false instead of defining bool operator?

二次信任 提交于 2019-12-02 19:22:09
I've been reading about overloading true and false in C#, and I think I understand the basic difference between this and defining a bool operator. The example I see around is something like: public static bool operator true(Foo foo) { return (foo.PropA > 0); } public static bool operator false(Foo foo) { return (foo.PropA <= 0); } To me, this is the same as saying: public static implicit operator bool(Foo foo) { return (foo.PropA > 0); } The difference, as far as I can tell, is that by defining true and false separately, you can have an object that is both true and false, or neither true nor

Creating methods with infinite parameters?

两盒软妹~` 提交于 2019-12-02 17:23:33
In C# you can do this: foo = string.Format("{0} {1} {2} {3} ...", "aa", "bb", "cc" ...); This method Format() accepts infinite parameters, being the first one how the string should be formatted and the rest are values to be put in the string. Today I've come to a situation where I had to get a set of strings and test them, then I remembered this language functionality, but I had no clue. After a few unsuccessful web searches, I've realised it would be more prudent to just get an array, which didn't make me quite satisfied. Q: How do I make a function that accepts infinite parameters? And how

Why does the compiler match “char” to “int” but not “short”?

天大地大妈咪最大 提交于 2019-12-02 16:28:13
I've got a small program: #include<iostream> using namespace std; void f(int) { cout << "int\n"; } void f(short) { cout << "short\n"; } int main(void){ char c = 0; f(c); return 0; } It prints int . I felt that, if this is because of "Integer promotion", why is not short preferred? I also know that integer promotion happens in an expression (like A=B). But I don't have expression in call to f(), right? If this is related to overload resolution rule, why passing char to f will result into compilers preferring int to short ? If I delete f(int) , then f(c) will call f(short) ! So in summary, my

Overloading multiple function objects by reference

浪尽此生 提交于 2019-12-02 16:21:59
In C++17, it is trivial to implement an overload(fs...) function that, given any number of arguments fs... satisfying FunctionObject , returns a new function object that behaves like an overload of fs... . Example: template <typename... Ts> struct overloader : Ts... { template <typename... TArgs> overloader(TArgs&&... xs) : Ts{forward<TArgs>(xs)}... { } using Ts::operator()...; }; template <typename... Ts> auto overload(Ts&&... xs) { return overloader<decay_t<Ts>...>{forward<Ts>(xs)...}; } int main() { auto o = overload([](char){ cout << "CHAR"; }, [](int) { cout << "INT"; }); o('a'); //

Boxing+Varargs is preferred over Boxing+Widening

别来无恙 提交于 2019-12-02 15:49:38
问题 Please correct me if I'm wrong. Is Boxing+Varargs is preferred over Boxing+Widening? I found in a site that its the other way. 回答1: What method is called when several could qualify is defined in the JLS #15.2.2: The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase. The second phase (§15.12.2.3)

Methods with the same name in one class in python?

痴心易碎 提交于 2019-12-02 15:27:42
How to declare few methods with the same name ,but with different numbers of parameters or different types in one class? What I must to change in this class: class MyClass: """""" #---------------------------------------------------------------------- def __init__(self): """Constructor""" def my_method(self,parameter_A_that_Must_Be_String): print parameter_A_that_Must_Be_String def my_method(self,parameter_A_that_Must_Be_String,parameter_B_that_Must_Be_String): print parameter_A_that_Must_Be_String print parameter_B_that_Must_Be_String def my_method(self,parameter_A_that_Must_Be_String

Which Overloaded Method is Called in Java

风流意气都作罢 提交于 2019-12-02 15:22:49
I have a basic inheritance situation with an overloaded method in the super class. public class Person { private String name; private int dob; private String gender; public Person(String theName, int birth, String sex){ name = theName; dob = birth; gender = sex; } public void work(){ getWorkDetail(this); } public void getWorkDetail(Employee e){ System.out.println("This person is an Employee"); } public void getWorkDetail(Person p){ System.out.println("This person is not an Employee"); } } The following Employee class extends the Person class above: public class Employee extends Person { String

Java Constructor Overloading

人走茶凉 提交于 2019-12-02 13:44:13
I'm new with Java and I'm having trouble understanding the constructor issue, I have looked at many tutorials and still I'm having difficult to understand why we use constructors, anyway, my specific question is : Correct me if I'm wrong, if i want to add in my class more than one constructor, I'll write the first one and the second will be int type (inside the brackets). is it because the constructors have to be with the same name as the class and we need to distinguish between them ? what if I want to add a third constructor ? Can it also be int type ? a) is it because the constructors have

Overloading method of comparison for custom class

流过昼夜 提交于 2019-12-02 12:57:52
问题 I want to overload methods of comparison for a personnal class. For example if I write this : $object1 < $object2 Php will use this function : function compare($a, $b){ if($a->attribute == $b->attribute){return 0;} else{return $a->attribute > $b->attribute ? 1 : -1;} } Is there a way to do this ? I already seen this and this but I can't use these solutions 回答1: The PECL solution you point to above is your only option. PHP does not provide operator overloading as available in other languages.

C# overload with override

半世苍凉 提交于 2019-12-02 12:14:06
I can surely answer to this question by myself writing a dummy test but I want to know what people think about the question. Here it is: Which method will be call when we have both at the same time overloading and overriding? I am only considering Type overloading and not arity overloading and when Type the overload are related. Let me throw you an example: class AA {} class BB : AA {} class A { public virtual void methodA(AA anAA) { Console.Write("A:methodA(AA) called"); } public virtual void methodA(BB aBB) { Console.Write("A:methodA(BB) called"); } } class B : A { public override void