overloading

Automatic type conversion in Java?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 19:55:57
Is there a way to do automatic implicit type conversion in Java? For example, say I have two types, 'FooSet' and 'BarSet' which both are representations of a Set. It is easy to convert between the types, such that I have written two utility methods: /** Given a BarSet, returns a FooSet */ public FooSet barTOfoo(BarSet input) { /* ... */ } /** Given a FooSet, returns a BarSet */ public BarSet fooTObar(FooSet input) { /* ... */ } Now say there's a method like this that I want to call: public void doSomething(FooSet data) { /* .. */ } But all I have is a BarSet myBarSet ...it means extra typing,

C++ Protected / Public overloads

混江龙づ霸主 提交于 2019-12-01 19:47:40
I have a class like this : class Foo { public: Foo() { for(int i = 0; i < 10; ++i) v.push_back(i); }; const vector<double>& V() const {return v;}; protected: vector<double>& V() {return v;}; private: vector<double> v; }; And then a piece of code like this : Foo foo; for(int i = 0; i < (int) foo.V().size(); ++i) cout << foo.V().at(i) << endl; However, the latter raises a compilation error saying the V() call is a protected method while i am just trying to read from it, not modify it. I have tried the following (but without success). Foo foo; const vector<double>& test = foo.V(); for(int i = 0;

Undefined number of arguments in C++ [duplicate]

此生再无相见时 提交于 2019-12-01 19:16:43
问题 This question already has answers here : Variable number of arguments in C++? (16 answers) Closed 6 years ago . Can I overload my function to do something with a lot of arguments as in JavaScript . For example: function f() { alert(arguments[0]); } f(4); // will alert 4 Can I do the same thing in C++ ? 回答1: You can use variadic template arguments and tuples: #include <tuple> #include <iostream> template <class... Args> void f(Args&&... args) { auto arguments = std::make_tuple(std::forward

Forcing std::vector overload instead of int overload on list with one element

北战南征 提交于 2019-12-01 19:05:19
Consider the code below: #include <iostream> #include <vector> void f(std::vector<int> v) {std::cout << __PRETTY_FUNCTION__ << std::endl;} void f(int n) {std::cout << __PRETTY_FUNCTION__ << std::endl;} int main() { f({42}); // the int overload is being picked up } Live on Coliru I was a bit surprised to realize that in this case the int overload is being picked up, i.e. the output of the program is: void f(int) with the warning warning: braces around scalar initializer [-Wbraced-scalar-init] f({42}); Of course this happens only when I pass a 1-element list as an argument, otherwise the std:

Overloading function using varargs

三世轮回 提交于 2019-12-01 18:10:47
This will not compile: public class Methods { public static void method(Integer... i) { System.out.print("A"); } public static void method(int... i) { System.out.print("B"); } public static void main(String args[]) { method(7); } } This will compile and work: public class Methods { public static void method(Integer i) { System.out.print("A"); } public static void method(int i) { System.out.print("B"); } public static void main(String args[]) { method(7); } } First and second example are very similar. First uses varargs, second not. Why one works, second not. 7 is primitive, so second method

For an overloaded function, calling specialized version for parent and child instances

冷暖自知 提交于 2019-12-01 18:02:29
I asked a question earlier but it turns out my problem was not properly modeled by my example. So here is my actual problem: I have class A , and class B inheriting from A , I have two functions foo(A&) and foo(B&) , I have a list of A* pointers, containing instances of A and B . How do I get to call foo(A&) for instances of A and foo(B&) for instances of B ? Constraints: I can modify A and B implementation, but not foo 's implementation. See below an example: #include <iostream> #include <list> class A { public: }; class B : public A { public: }; void bar(A &a) { std::cout << "This is an A" <

How to call the more specific method of overloading

百般思念 提交于 2019-12-01 17:52:37
Here is an example playground: protocol P { associatedtype T func getValue() -> T } class Foo: P { func getValue() -> String { return "hello" } } class Bar { func test<T: P>(_ o: T) { print("Generic", o.getValue()) } func test(_ o: Any) { print("Any") } } let foo = Foo() let bar = Bar() bar.test(foo) This outputs: Any . If I remove the Any version of test , the generic method is called. Class Foo conforms to protocol P , why does Swift not pick the generic method since it is more specific? Is there a way to call the generic one? Hamish From what I understand, the compiler will always favour an

Advantages of Constructor Overloading

泪湿孤枕 提交于 2019-12-01 17:51:55
I am very new to Java and trying to learn the subject, having previous programming exposure in only HTML/CSS. I have started with Herbert Schildt and progressed through a few pages. I am unable to understand the exact advantages of Constructor Overloading. Isn't it easier to Overload Methods using single constructor for flexibility? Moreover if I am trying to use constructor overloading to use one object to initialize another, there are simpler ways to do it! So what are the benefits and in which situation should I use Constructor Overloading. Constructor overloading is very useful for

operator<< overloading ostream

隐身守侯 提交于 2019-12-01 17:48:45
In order to use cout as such : std::cout << myObject, why do I have to pass an ostream object? I thought that was an implicit parameter. ostream &operator<<(ostream &out, const myClass &o) { out << o.fname << " " << o.lname; return out; } Thanks You aren't adding another member function to ostream , since that would require redefining the class. You can't add it to myClass , since the ostream goes first. The only thing you can do is add an overload to an independent function, which is what you're doing in the example. Only if it is a member function of the class that would otherwise be the

How can I distinguish overloads of templates with non-type parameters?

孤者浪人 提交于 2019-12-01 17:44:28
Here are two template functions, that differ only in their template parameters. The rest of the parameters are exactly the same. template<int module> void template_const(int &a,int & b){ a = a & module; b = b % module; } template<bool x> void template_const(int &a,int & b){ int w; if (x){ w = 123; } else w = 512; a = a & w; b = b % w; } When I try to call them like this template_const<true>(a,b) or template_const<123>(a,b) the compiler tells me that the call is ambiguous. How can I call these two functions? Luc Touraille As @jogojapan pointed out, the problem is that the compiler cannot order