overloading

Why can't I assign a scalar value to a class using shorthand, but instead declare it first, then set its value?

馋奶兔 提交于 2020-01-03 14:07:22
问题 I am writing a UTF-8 library for C++ as an exercise as this is my first real-world C++ code. So far, I've implemented concatenation, character indexing, parsing and encoding UTF-8 in a class called "ustring". It looks like it's working, but two seemingly equivalent ways of declaring a new ustring behave differently. The first way: ustring a; a = "test"; works, and the overloaded "=" operator parses the string into the class (which stores the Unicode strings as an dynamically allocated int

Why can't I assign a scalar value to a class using shorthand, but instead declare it first, then set its value?

假如想象 提交于 2020-01-03 14:06:11
问题 I am writing a UTF-8 library for C++ as an exercise as this is my first real-world C++ code. So far, I've implemented concatenation, character indexing, parsing and encoding UTF-8 in a class called "ustring". It looks like it's working, but two seemingly equivalent ways of declaring a new ustring behave differently. The first way: ustring a; a = "test"; works, and the overloaded "=" operator parses the string into the class (which stores the Unicode strings as an dynamically allocated int

Why is it not possible to override operator<< for template classes involving third-party code?

守給你的承諾、 提交于 2020-01-03 09:07:23
问题 I asked about the following in https://stackoverflow.com/a/51951315/1908650 : I want to overload template<class T> ostream& operator<<(ostream& os, const optional<unique_ptr<T>>& ). In the comments, @Yakk - Adam Nevraumont notes: The answer to that question is "you cannot". There is no good legal way to do that for a generic type T; I could explain why, but it would take a new question/answer to do so I'm creating a new Q. to take up the kind offer... 回答1: The proper place to overload

Does dart support operator overloading

本秂侑毒 提交于 2020-01-03 07:06:32
问题 I read that Dart does not support function overloading. Does it support operator overloading. If yes, would be kind and show me how in a simple example how its done. And what are some advantages etc. I am new to programming. Thanks. 回答1: The chosen answer is no longer valid when you try overloads the == operator in new version .Now you need do like this: class MyClass { @override bool operator ==(other) { // compare this to other } } But it's not safe. other is not specified as a type,

Overload function int… and long… simultaneously

走远了吗. 提交于 2020-01-02 12:24:51
问题 I want to create two functions, say long min(long...); int min(int...); But when I try to invoke the second i.e min(1, 5) one I get ambiguous method call Is there workaround except renaming? 回答1: It is a known bug The behaviour you describe is a bug which has been fixed with Java 7. See details in the release notes, section called "Changes in Most Specific Varargs Method Selection". The reason why it should compile Variable arity comes last in determining the most specific method. The rules

Java overloaded method in library fails when not run in web server

橙三吉。 提交于 2020-01-02 09:41:53
问题 I am trying to write a small library that can either be used in a standard java app or as part of a servlet. I have defined a couple of overloaded methods as follows: // imports etc. public ExampleLibrary { /** * This one is meant to be used by a J2SE app */ public String processData(Map headers) throws MyException { // process // return result } /** * This one is meant to be used by a servlet */ public String processData(HttpServletRequest request) throws MyException { // extract headers

Java overloaded method in library fails when not run in web server

霸气de小男生 提交于 2020-01-02 09:41:37
问题 I am trying to write a small library that can either be used in a standard java app or as part of a servlet. I have defined a couple of overloaded methods as follows: // imports etc. public ExampleLibrary { /** * This one is meant to be used by a J2SE app */ public String processData(Map headers) throws MyException { // process // return result } /** * This one is meant to be used by a servlet */ public String processData(HttpServletRequest request) throws MyException { // extract headers

Best way to “overload” function in python? [duplicate]

断了今生、忘了曾经 提交于 2020-01-02 08:39:07
问题 This question already has answers here : Function overloading in Python: Missing [closed] (5 answers) Closed 4 years ago . I am trying to do something like this in python: def foo(x, y): # do something at position (x, y) def foo(pos): foo(pos.x, pos.y) So I want to call a different version of foo depending on the number of parameters I provide. This of course doesn't work because I redefined foo . What would be the most elegant way of achieving this? I would prefer not to use named parameters

Does implicit operator have higher priority over ToString() method? [duplicate]

早过忘川 提交于 2020-01-02 05:22:09
问题 This question already has an answer here : Order of implicit conversions in c# (1 answer) Closed last year . Consider the following code: public class Test { public static implicit operator int(Test t) { return 42; } public override string ToString() { return "Test here!"; } } var test = new Test(); Console.WriteLine(test); // 42 Console.WriteLine((Test)test); // 42 Console.WriteLine((int)test); // 42 Console.WriteLine(test.ToString()); // "Test here!" Why in the first three cases we have

Java: Overriding or Overloading method?

[亡魂溺海] 提交于 2020-01-02 04:48:05
问题 I have a method, in a class called "PlaceParser" that extends "ModelParser": protected Place parseModel(JSONObject element) ... A Place is a sub class of Model. Should the @Override annotation be added to the above code? As the method has a different return type, does this still count as overriding the base class method with the same name and arguments / does the return type alter the 'signature'? The "ModelParser" method looks like this "ModelT" also extends "Model": protected abstract