overloading

How function overloading works with double and float

依然范特西╮ 提交于 2019-12-11 06:16:20
问题 I have two overloaded methods double Sum(double n1, double n2) { return n1 + n2; } float Sum(float n1, float n2) { return n1 + n2; } When i call Sum(5.5, 5.5), the method with double return type gets called. My question is why the method with double return type is called, why not method with float return type. How the compiler is deciding that which method should be called. 回答1: Because, floating-point literals such as 5.5 have the type double in C++ as default. That's why, when you pass a

Java: Function with one varargs argument and function with same name and one argument of same type is allowed? [duplicate]

穿精又带淫゛_ 提交于 2019-12-11 05:58:41
问题 This question already has answers here : Why does type-promotion take precedence over varargs for overloaded methods (2 answers) Closed last year . While preparing for Java certification exam I was quite surprised to see that Java allows this: public class Consumer { public void buy(Object o) { System.out.println("Buying one object"); } public void buy(Object... o) { System.out.println("Buying multiple objects"); } public static void main(String[] args) { Consumer consumer = new Consumer();

Variadic template function overloading

岁酱吖の 提交于 2019-12-11 05:57:38
问题 I have a class with a variadic template member function (foo) like below. The idea is to skip all doubles in the parameter and allocate an object with user provided arguments. template <class T> class Var { public: template <typename U, typename ...Args> int foo(int index, Args... args) { T* p = new U(args...); // save in an array at index 'index' } template <typename U, typename ...Args> int foo (double index, Args... args) { // do something with index and skip it return foo<U>(args...); } }

calling java varargs from scala with overloading

时光怂恿深爱的人放手 提交于 2019-12-11 05:14:29
问题 It's another question about scala-java compatibility related to varargs feature. The key difference is that java's part is overloaded. It resembles this scala code: object Test { def test( xa : String* ) = print( xa.mkString(",") ) def test( xs : Seq[String] ) = print( xs.mkString(",") ) } Scala unlike java mark such overloading as invalid error: double definition: def test(xa: String*): Unit at line 11 and def test(xs: Seq[String]): Unit at line 12 have same type after erasure: (xa: Seq)Unit

Adding Overloaded Constructors to Implicit F# Type

我的未来我决定 提交于 2019-12-11 05:08:41
问题 I have created the following type using implicit type construction: open System type Matrix(sourceMatrix:double[,]) = let rows = sourceMatrix.GetUpperBound(0) + 1 let cols = sourceMatrix.GetUpperBound(1) + 1 let matrix = Array2D.zeroCreate<double> rows cols do for i in 0 .. rows - 1 do for j in 0 .. cols - 1 do matrix.[i,j] <- sourceMatrix.[i,j] //Properties ///The number of Rows in this Matrix. member this.Rows = rows ///The number of Columns in this Matrix. member this.Cols = cols //

How to pass a member function pointer to an overloaded method in a template function?

跟風遠走 提交于 2019-12-11 03:57:38
问题 I referred to this somewhat similar question. However here the scenario is different: struct A { void foo (int i) {} // choice void foo (double i) {} }; template<typename ObjType, typename FuncPtr> void ReceiveFuncPtr (ObjType o, FuncPtr pf) { (o.*pf)(1); } int main () { A obj; ReceiveFuncPtr(obj, &A::foo); // don't want typecast here } In the above test code, I have an overloaded foo inside A . Had there been only 1 foo then the code works fine. But for overloading case, compiler complains

Overload division in C#

不想你离开。 提交于 2019-12-11 03:54:48
问题 I want to overload division operator in my C# class. So, i wrote: public string[] operator/ (object obj) { } And got error: "Parser error: Overloadable unary operator excepted". So, i cant overload that operator? On the MSDN i don't see any example: http://msdn.microsoft.com/en-us/library/3b1ff23f.aspx Thanks. //i'm using MonoDevelop on Ubuntu 14.10, if it's needed. 回答1: You can overload the division operator, but: It must always be a binary operator - you've only provider one operand It must

How Can I Use result_of Instead of decltype?

故事扮演 提交于 2019-12-11 03:46:51
问题 In this answer I create a type trait: template<typename T> using to_string_t = decltype(to_string(declval<T>())); This works just fine but I originally set out to use result_of and now it's irking me that I can't figure out how to do it. I'm trying to replace the line above with something like this: template<typename T> using to_string_t = result_of<to_string(T)>; But I get a compiler error along the lines of: error C2275: 'T': illegal use of this type as an expression note: see declaration

C++ newbie question: designing a function to return a vector of either string or double--overload or template?

别等时光非礼了梦想. 提交于 2019-12-11 03:14:23
问题 I have written a function that searches a text file for names. It returns vector, where each element of the vector is a different name. Now I would like to search the same text file for numbers and return the numbers in a vector. This is probably a dumb question, but I'm wondering what the best approach would be. Overload the function by writing a second function that returns a vector or turning the function I have already written into a template by replacing the type with T, as in vector.

Overloading a method in com visible dll

荒凉一梦 提交于 2019-12-11 03:06:41
问题 I am creating a COM visible dll and I was trying to overload a method. So basically this code: [ComVisible(true)] [ProgId("TAF.TextLog")] [Guid("af3f89ed-4732-4367-a222-2a95b8b75659")] public class TextLog { String _logFilePath; public TextLog() { } [ComVisible(true)] public void Create(string filePath) { String path = Path.GetDirectoryName(filePath); if (Directory.Exists(path)) { _logFilePath = filePath; } [ComVisible(true)] public void Write(string message) { WriteMessage(null, message,