overloading

Method overload selection with null

你离开我真会死。 提交于 2019-12-19 08:38:30
问题 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

Priority between normal function and Template function

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 07:06:08
问题 In the following code, the main function uses normal function instead of Template function. #include <iostream> using namespace std; template <class T> void num(T t){cout<<"T : "<<t;} void num(int a){cout<<"wT : "<<a;} int main() { num(5); return 0; } What is the possible reason behind this? 回答1: To call the template method in this case, you need to invoke the method explicitly with num<int>(5) instead of num(5) . Although the compiler can infer, non-generic method is preferred to a generic

How do I use a chain of operator overloads without modifying the operands?

放肆的年华 提交于 2019-12-19 07:04:25
问题 Let's say we have this class A: class A { public: int a; A(int b) { a = b; } }; I would like to create a + overload such that I could use it like this A a(1),b(2),c(3),&d; d = a + b + c; without modifying the content of each object. The next logical thing would be allocating a new chunk of memory each time like this: A &operator+ (const A &b) { A *c = new A(a+b.a); return *c; } But this would create a new problem: intermediate results are lost, causing memory leaks. I could have easily solved

When the C++ standard provides C headers bringing names into the global namespace, does that include overloads?

三世轮回 提交于 2019-12-19 06:57:13
问题 The final committee draft of the upcoming C++0x standard says: Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3). Earlier C++

When the C++ standard provides C headers bringing names into the global namespace, does that include overloads?

好久不见. 提交于 2019-12-19 06:55:35
问题 The final committee draft of the upcoming C++0x standard says: Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3). Earlier C++

How to overload the 'new' method?

拥有回忆 提交于 2019-12-19 04:06:31
问题 I'm just started to learn Rust and I'm wondering if there is way to overload methods. At first I created a struct and used a 'impl' to implement basic 'new' method. Then I thought to add 'new' method with some params, and I tried to use trait for that. The following code was successfully compiled but once I tried to use 'new' with params, compiler gave me an error about extra params. So how should I overload methods in Rust? pub struct Words<'a> { pub nouns: Vec<&'a str>, } trait Test<'a>{ fn

Different overloads with std::function parameters is ambiguous with bind (sometimes)

别说谁变了你拦得住时间么 提交于 2019-12-19 04:01:08
问题 I have two overloads of a function foo which take different std::function s which results in an ambiguity issue for the latter when used with the result of a std::bind . I don't understand why only this is ambiguous. void foo(std::function<void(int)>) {} void foo(std::function<int()>) {} void take_int(int) { } int ret_int() { return 0; } When using int() with a bind function I get an ambiguity error foo(std::bind(ret_int)); // ERROR With the gcc-5.1 error (and similar with clang) error: call

.NET Overload WebMethods - Possible?

主宰稳场 提交于 2019-12-19 03:14:52
问题 I have two web methods which I wish to overload: <WebMethod()> _ Public Function GetProject(ByVal id As Int32) As Project <WebMethod(MessageName:="GetProjects")> _ Public Function GetProject(ByVal filter As String) As Projects I read about overloading using MessageName, however I cannot get this to work. Is this possible? 回答1: Off course it is possible! do not forget to change the WebServiceBinding [WebServiceBinding(ConformsTo = WsiProfiles.None)] try this: [WebService(Namespace = "http:/

Const temporary from template type and why use std::add_const?

天涯浪子 提交于 2019-12-18 21:17:30
问题 The following code is excerpted from cppreference.com. #include <iostream> #include <type_traits> struct foo { void m() { std::cout << "Non-cv\n"; } void m() const { std::cout << "Const\n"; } }; template <class T> void call_m() { T().m(); } int main() { call_m<foo>(); call_m<std::add_const<foo>::type>(); } However, when compiled with VC++ Nov 2012 CTP, the output is Non-cv Non-cv rather than the expected: Non-cv Const Besides, what's the difference between the following two statements: call_m

Is there a way to Overload a Property in .NET

好久不见. 提交于 2019-12-18 18:59:08
问题 I've done plenty of Method Overloading, but now I have an instance where I would like to Overload a Property. The IDE in Visual Studio seems to allow it, since I can actually set up the two overloads, but I get an error saying it is not valid because they only differ in type. I think I'm missing something in my syntax? I want to be able to use two (or more) different custom classes as the Type for my property. Public Overloads Property myFlexibleProperty() As myCustomClass1 Get Return