overloading

Why overload true and false instead of defining bool operator?

心不动则不痛 提交于 2019-12-03 05:01:33
问题 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

Why would anyone want to overload the & (address-of) operator? [duplicate]

不问归期 提交于 2019-12-03 04:55:12
This question already has answers here : What legitimate reasons exist to overload the unary operator&? (7 answers) Possible Duplicate: What legitimate reasons exist to overload the unary operator& ? I just read this question , and I can't help but wonder: Why would anyone possibly want to overload the & ("address-of") operator? some_class* operator&() const { return address_of_object ; } Is there any legitimate use case? If you're dealing with any sort of wrapper objects, you might want or need to transparently forward the access to the wrapper to the contained object. In that case, you can't

When is deleting a template instantiation preferable to deleting a non-template overload?

穿精又带淫゛_ 提交于 2019-12-03 04:50:06
Suppose I have a template that works with raw pointers: template<typename T> void processPointer(T* ptr); I don't want this to be called with void* pointers. It seems I have two choices. I can delete a non-template overload: void processPointer(void*) = delete; Or I can delete a template instantiation: template<> void processPointer<void>(void*) = delete; Declaring the non-template overload is easier (no futzing with angle brackets). Are there reasons why I'd prefer to delete the template instantiation instead? Here's one reason to favor the template version: processPointer<void>(void*) can

Creating methods with infinite parameters?

£可爱£侵袭症+ 提交于 2019-12-03 04:04:22
问题 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

Can I pass a primitive type by reference in Java? [duplicate]

纵饮孤独 提交于 2019-12-03 03:17:03
This question already has an answer here: How do I pass a primitive data type by reference? 8 answers I would like to call a method which could potentially take on different versions, i.e. the same method for input parameters that are of type: boolean byte short int long The way I would like to do this is by "overloading" the method (I think that is the correct term?): public void getValue(byte theByte) {...} public void getValue(short theShort) {...} ... etc ... ... but that would mean that I would have to pass the primitive type in by reference... similar to C++ where the method has external

Use invokedynamic to implement multiple dispatch

你。 提交于 2019-12-03 03:10:09
I wondered if Java7's new invokedynamic bytecode instruction could be used to implement multiple dispatch for the Java language. Would the new API under java.lang.invoke be helpful to perform such a thing? The scenario I was thinking about looked as follows. (This looks like an application case for the visitor design pattern, but there may be reasons that this is not a viable option.) class A {} class A1 extends A {} class A2 extends A {} class SomeHandler { private void doHandle(A1 a1) { ... } private void doHandle(A2 a2) { ... } private void doHandle(A a) { ... } public void handle(A a) {

Which Overloaded Method is Called in Java

醉酒当歌 提交于 2019-12-03 03:01:39
问题 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

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

﹥>﹥吖頭↗ 提交于 2019-12-03 02:54:26
问题 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

ruby operator overloading question

老子叫甜甜 提交于 2019-12-03 02:26:54
i've been messing around with ruby and opengl for entertainment purposes, and i decided to write some 3d vector/plane/etc classes to pretty up some of the math. simplified example: class Vec3 attr_accessor :x,:y,:z def *(a) if a.is_a?(Numeric) #multiply by scalar return Vec3.new(@x*a, @y*a, @z*a) elsif a.is_a?(Vec3) #dot product return @x*a.x + @y*a.y + @z*a.z end end end v1 = Vec3.new(1,1,1) v2 = v1*5 #produces [5,5,5] which all fine and dandy, but i also want to be able to write v2 = 5*v1 which requires adding functionality to Fixnum or Float or whatever, but i couldn't find a way to

Overload and hide methods in Java

喜你入骨 提交于 2019-12-03 02:11:53
i have an abstract class BaseClass with a public insert() method: public abstract class BaseClass { public void insert(Object object) { // Do something } } which is extended by many other classes. For some of those classes, however, the insert() method must have additional parameters, so that they instead of overriding it I overload the method of the base class with the parameters required, for example: public class SampleClass extends BaseClass { public void insert(Object object, Long param){ // Do Something } } Now, if i instantiate the SampleClass class, i have two insert() methods: