overloading

Method Overloading with Optional Parameter

北城以北 提交于 2019-12-01 13:42:51
问题 I have a class as follows with two overload method. Class A { public string x(string a, string b) { return "hello" + a + b; } public string x(string a, string b, string c = "bye") { return c + a + b; } } If I call the method x from another class with two parameters, then which method is going to execute and why? i.e, string result = new A().x("Fname", "Lname"); I've tested this in my console application and the method with 2 parameters execute. Can someone explain this? 回答1: Use of named and

Overloaded function type in typescript

丶灬走出姿态 提交于 2019-12-01 12:19:24
问题 How can I create a function type, without providing a concrete function, that is overloaded? By inspecting the type of an overloaded function, it seems multiple call signatures on an interface/object type are the way to go: function a(input: string): string function a(input: number): number function a(input: string | number): string | number { return input } type A = typeof a type B = { (input: string): string (input: number): number } const b: B = a // Okay! Defining the same idea with a

C++ template operator overloading with different types

白昼怎懂夜的黑 提交于 2019-12-01 11:27:20
The example below defines a basic podtype container class. Using this class a series of typedefs are then created which represent an OOP version of the basic podtype. The problem originates when we start assigning those types to one another. I tried to define the operator as friend method with lhs and rhs arguments using plain PodObjects as type but without any succes. Is there anyone who might have experienced something simular or knows an other solution for this problem. Thanks in advance. #include <stdint.h> template <typename T> class PodObject { protected: T _value; public: PodObject<T>

Overloading with variable args

半腔热情 提交于 2019-12-01 10:52:38
问题 class OverloadingVarargs2 { static void f(float i, Character... args) { System.out.println("first"); System.out.println(i); } static void f(Character... args) { System.out.println("second"); } static void test() { f(1, 'a'); f('b', 'c'); // the method f is ambiguous } } This code can't be compiled, The compiler says that f is ambiguous. But I think the second method can match f('b', 'c'); what's the problem? 回答1: That is because there is no way to determine if that method call should either

Java Method Overloading [duplicate]

限于喜欢 提交于 2019-12-01 09:24:53
This question already has an answer here: Why does type-promotion take precedence over varargs for overloaded methods 2 answers the following method returns output : in primitive int arg method public class TestMethodOverloading { private void show(int a){ System.out.println("in primitive int arg method"); } private void show(float a){ System.out.println("in primitive float arg method"); } public static void main(String[] args) { TestMethodOverloading tmo = new TestMethodOverloading(); tmo.show(4); } } Whereas if i change the arguement type of show method from int to Integer then output

Overloading << operator for std::ostream

懵懂的女人 提交于 2019-12-01 09:08:45
ostream& operator <<(ostream& osObject, const storageRentals& rentals) { osObject << rentals.summaryReport(); return osObject; } summaryReport() is a void function, and it is giving me an error: no operator "<<" matches these operands but the error is not there if I change the summaryReport function to an int , but the problem I have with that is you have to return a value, and it is printing it out on the screen. void storageRentals::summaryReport() const { for (int count = 0; count < 8; count++) cout << "Unit: " << count + 1 << " " << stoUnits[count] << endl; } Is there any way to overload

overloading assignment operator With subscript operator

有些话、适合烂在心里 提交于 2019-12-01 09:03:10
问题 I overloaded both subscript operator and assignment operator and I am trying to get right value to assignment operator example Array x; x[0]=5; by overloading subscript operator i can get value 0 but when i overload assignment operator it does the assignment but it doesn't use my overloaded function because vaiable 2 should have value 5. class Array { public: int *ptr; int one,two; Array(int arr[]) { ptr=arr; } int &operator[](int index) { one=index; return ptr[index]; } int & operator=(int x

Why does unique_ptr overload reset(pointer p = pointer()) and reset(nullptr_t)?

放肆的年华 提交于 2019-12-01 08:42:10
Accroding to http://en.cppreference.com/w/cpp/memory/unique_ptr/reset , void reset( pointer ptr = pointer() ); template< class U > void reset( U ) = delete; void reset( std::nullptr_t p ); 1) Given current_ptr , the pointer that was managed by *this , performs the following actions, in this order: Saves a copy of the current pointer old_ptr = current_ptr ; Overwrites the current pointer with the argument current_ptr = ptr ; If the old pointer was non-empty, deletes the previously managed object if(old_ptr != nullptr) get_deleter()(old_ptr) . 2) In the specialization for dynamic arrays, std:

Why does unique_ptr overload reset(pointer p = pointer()) and reset(nullptr_t)?

蓝咒 提交于 2019-12-01 07:21:29
问题 Accroding to http://en.cppreference.com/w/cpp/memory/unique_ptr/reset, void reset( pointer ptr = pointer() ); template< class U > void reset( U ) = delete; void reset( std::nullptr_t p ); 1) Given current_ptr , the pointer that was managed by *this , performs the following actions, in this order: Saves a copy of the current pointer old_ptr = current_ptr ; Overwrites the current pointer with the argument current_ptr = ptr ; If the old pointer was non-empty, deletes the previously managed

Method overload selection with null

坚强是说给别人听的谎言 提交于 2019-12-01 07:00:37
Given this code: class Overloading extends Object { static public void target(Object val, String chk) { System.out.println("Object["+val+"] :: Should be "+chk); } static public void target(String val, String chk) { System.out.println("String["+val+"] :: Should be "+chk); } static public void main(String[] args) { Object obj=null; target(null ,"Object"); target((Object)null,"Object"); target(obj ,"Object"); } } the output is (unexpectedly) as follows: String[null] :: Should be Object Object[null] :: Should be Object Object[null] :: Should be Object The problem is with the first line, which I