overloading

C++ - How to introduce overload set from variadic number of bases.

瘦欲@ 提交于 2019-11-30 17:58:21
The derived class hides the name of an overload set from the base class if the derived class has the same name defined, but we can always introduce that overload set back with using-declaration: template <class BASE> class A : public BASE { public: using BASE::some_method; void some_method(); } But what if I introduce all overload sets from variadic base classes? Would I be able to write something like this? template <class... BASES> class A : public BASES... { public: using BASES::some_method...; void some_method(); } I've considered using a helper class like: template <class... BASES> struct

Overload a method or use default values? c++

末鹿安然 提交于 2019-11-30 17:54:41
I'm still relatively new to C++ and I can't seem to figure out the difference in the following two ways of coding a function that may take one parameter or maybe two or three or more. Anyway, here's my point function overload: int aClass::doSomething(int required) { //DO SOMETHING } int aClass::doSomething(int required, int optional) { //DO SOMETHING } how is this different to, default value: int aClass::doSomething(int required, int optional = 0) { //DO SOMETHING } I know in different circumstances one may be more suitable than another but what kinds of things should I be aware of when

How do I call overloaded Java methods in Clojure

こ雲淡風輕ζ 提交于 2019-11-30 17:40:41
For this example Java class: package foo; public class TestInterop { public String test(int i) { return "Test(int)"; } public String test(Object i) { return "Test(Object)"; } } When I start Clojure and try to call the test(int) method, the test(Object) method is called instead, because Clojure automatically boxes the integer into a java.lang.Integer object. How do I force Clojure to call the test(int) method? user=> (.test (new foo.TestInterop) 10) "Test(Object)" I want to call methods like Component.add(Component comp, int index) in AWT, but instead keep calling add(Component comp, Object

C++ Template overload with enable_if: different behaviour with g++ and clang

拜拜、爱过 提交于 2019-11-30 17:34:43
During resolution of an overload of a templated member function of a base class, I observed a different behaviour between g++ (5.2.1-23) and clang (3.8.0), with -std=c++14 . #include <iostream> #include <type_traits> struct Base { template <typename T> auto a(T t) -> void { std::cout<< "False\n"; } }; template <bool Bool> struct Derived : public Base { using Base::a; template <typename T, bool B = Bool> auto a(T t) -> std::enable_if_t<B, void> { std::cout<< "True\n"; } }; int main() { Derived<true> d; d.a(1); // fails with g++, prints "true" with clang Derived<false> d2; d2.a(1); // fails with

Is there a way to Overload a Property in .NET

拈花ヽ惹草 提交于 2019-11-30 17:34:11
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 _myFlexibleProperty1 End Get Set(ByVal value As myCustomClass1) _myFlexibleProperty1 = value End Set End

Java overloading method selection

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 17:34:08
I'm trying to get my head round how Java selects which method is executed: //Example 1 prints Square:add(Figure) Figure fs = new Square(); fs.add(fs); //Example 2 prints Square:add(Figure) Rectangle rs = new Square(); rs.add(fs); //Example 3 prints Rectangle:add(Rectangle). Expected Square:add(Square) rs.add(new Square()); //Example 4 prints Rectangle:add(Rectangle). Expected Square:add(Figure) Square ss = new Square(); ss.add(rs); class Figure { public void add(Figure f){ System.out.println("Figure:add(Figure)"); } } class Rectangle extends Figure { @Override public void add(Figure f){ System

overloading on basis of return type only

喜你入骨 提交于 2019-11-30 17:29:17
问题 I have a situation where i want to return List<> from this function public DataTable GetSubCategoriesBySubCatID(Guid SubCategoryID) So what i want is public List<SubCategories> GetSubCategoriesBySubCatID(Guid SubCategoryID) I know overloading is not supported on the basis of return type only,I just donot want to duplicate same code in both functions. Whats the best way to achieve this without impacting the references which hold true for first function 回答1: Give them different names: public

How to write Java function that returns values of multiple data types?

纵然是瞬间 提交于 2019-11-30 17:21:22
For example, I want to create a function that can return any number (negative, zero, or positive). However, based on certain exceptions, I'd like the function to return Boolean FALSE Is there a way to write a function that can return an int or a Boolean ? Ok, so this has received a lot of responses. I understand I'm simply approaching the problem incorrectly and I should throw some sort of Exception in the method. To get a better answer, I'm going to provide some example code. Please don't make fun :) public class Quad { public static void main (String[] args) { double a, b, c; a=1; b=-7; c=12

Subscript: access my dictionary values with a String enumeration

↘锁芯ラ 提交于 2019-11-30 17:03:55
I want to do something like that: access my dictionary values with a String enumeration. I am trying to overload the subscript of the dictionary but without success. Accessing the dictionary: let district = address[JsonKeys.district] where JsonKeys is: enum JsonKeys: String { case key1 case key2 case key... } and my subscript overload is as follow: extension Dictionary where Key: StringLiteralConvertible, Value: AnyObject { subscript(index: FOJsonKeys) -> AnyObject { get { return self[ index.rawValue] as! AnyObject } } } I get the following message: **Cannot subscript a value of type

Java overloading method selection

寵の児 提交于 2019-11-30 16:41:23
问题 I'm trying to get my head round how Java selects which method is executed: //Example 1 prints Square:add(Figure) Figure fs = new Square(); fs.add(fs); //Example 2 prints Square:add(Figure) Rectangle rs = new Square(); rs.add(fs); //Example 3 prints Rectangle:add(Rectangle). Expected Square:add(Square) rs.add(new Square()); //Example 4 prints Rectangle:add(Rectangle). Expected Square:add(Figure) Square ss = new Square(); ss.add(rs); class Figure { public void add(Figure f){ System.out.println(