overloading

C++ - Constructor overloading - private and public

Hi All, Can you tell me why the following code is giving me the following error - call of overloaded "C(int)" is ambiguous I would think that since C(char x) is private, only the C(float) ctor is visible from outside and that should be called by converting int to float. But that's not the case. class C { C(char x) { } p...

How to change this design to avoid a downcast?

Let's say I have a collection of objects that all inherit from a base class. Something like... abstract public class Animal { } public class Dog :Animal { } class Monkey : Animal { } Now, we need to feed these animals, but they are not allowed to know how to feed themselves. If they could, the...

how explicit should I be with my overloads?

I'm building a wrapper for a jquery plugin to C# and I dont like the usage of [Optional] because its not as "optional" as it says it is(meaning you still have to declare that System.Missing library) so I decided to use overloaded methods. I want to give the user alot of customization but I'm not sure how explicit I should be with my over...

Optional function parameters: Use default arguments (NULL) or overload the function?

I have a function that processes a given vector, but may also create such a vector itself if it is not given. I see two design choices for such a case, where a function parameter is optional: Make it a pointer and make it NULL by default: void foo(int i, std::vector<int>* optional = NULL) { if(optional == NULL){ optional = new...

C++: How to get the address of an overloaded member function?

Hi, I'm trying to get a pointer to a specific version of an overloaded member function. Here's the example: class C { bool f(int) { ... } bool f(double) { ... } bool example() { // I want to get the "double" version. typedef bool (C::*MemberFunctionType)(double); MemberFunctionType pointer = &C::f; // <- Visual C...

Specialize implementation of GenericType<A,B> for case A == B?

I have a generic class which takes two type parameters, Generic<A, B>. This class has methods with signatures that are distinct so long and A and B are distinct. However, if A == B the signatures match exactly and overload resolution cannot be performed. Is it possible to somehow specify a specialisation of the method for this case? ...

Function overloading in Python: Missing

As this says: http://mail.python.org/pipermail/python-list/2003-May/206149.html Function overloading is absent in Python. As far as I feel this a big handicap since its also an OO language. Initially I found that unable to differentiate between the argument types was difficult but the dynamic nature of Python made it easy (e.g. list, ...

C++ functions accepting both pointers and references

I am writing a library in C++ and have some functions that work with modules. An example would look like this: void connect(Module *a, Module *b); The problem is, that it would be sometimes handy if the function accepted also references (some of the Modules may be allocated on the stack and some on the heap and all the &s and *s get b...

Overload "base" contructor or "this" contructor?

I have few types that derive from simplified Base as shown below. I am not sure whether to use base class's constructor or this constructor when overloading constructors. ConcreteA overloads constructors purely using base constructors, while ConcreteB overloads using this for the first two overloads. What would be a better way of ove...

Java: which is faster overloading or if/else

Hi all, I have child classes, each carries a different type of value along with other members. There may be a LongObject, IntObject, StringObject, etc. I will be given a value, which can be a long, int, string, etc., and I have to create a LongObject, IntObject, StringObject, etc., respectively. Would it be faster to overload a met...

Java overloading vs overwriting

Hi I just want to make sure I have these concepts right. Overloading in java means that you can have a constructor or a method with different number of arguments or different data types. i.e public void setValue(){ this.value = 0; } public void setValue(int v){ this.value = v; } How about this method? Would it still be consider...

Java Interface: Inheriting, Overriding, and Overloading Methods

Hi All, In "THE Java™ Programming Language, Fourth Edition" By Ken Arnold, James Gosling, David Holmes, its mentioned that: paragraph: (4.3.2) "Similarly, if an interface inherits more than one method with the same signature, or if a class implements different interfaces containing a method with the same signature, there is only one su...

How to invoke an overloaded generic methods in IronRuby?

Hi, :) How can I invoke overloaded generic method in IronRuby? I have a .net class with the following methods. (Take note that the methods are static) Factory.cs ---- public static T CreateService<T>() public static T CreateService<T>(string serviceName) ironruby_sample.rb ---- service = Factory.create_service[ISomeService] => prod...

Does an overloaded __get need to manage all member variables?

I am creating a __get() function for a class to control access to my private member variables. Do I need to design the function to handle all possible member value reads or can I not write it for members that are public? Also, I am assuming that classes that inherit this class will use my __get() function to access private members. cl...

Template class expression parameter overloading

Hey, I'm trying to figure out if it's possible to "overload" a template class deffinition with expression parameters. Kind of like the following snippet of code. template<class T> class Test { public: T testvar; Test() { testvar = 5; cout << "Testvar: " << testvar << endl; } }; template<class T> class Test<T,...

Why does defining __getitem__ on a class make it iterable in python?

Why does defining __getitem__ on a class make it iterable? For instance if I write: class b: def __getitem__(self, k): return k cb = b() for k in cb: print k I get the output: 0 1 2 3 4 5 6 7 8 ... I would really expect to see an error returned from "for k in cb:" ...

Overloading a method that takes a generic list with different types as a parameter

How can I overload a method that takes a Generic List with different types as a parameter? For example: I have a two methods like so: private static List<allocations> GetAllocationList(List<PAllocation> allocations) { ... } private static List<allocations> GetAllocationList(List<NPAllocation> allocations) { ... } Is there a...

Method overloading return values

In C# I need to be able to define a method but have it return one or two return types. The compiler gives me an error when I try to do it, but why isn't it smart enough to know which method I need to call? int x = FunctionReturnsIntOrString(); Why would the compiler prevent me from having two functions with different return types? ...

How to write a C++ template that accepts every class and class template?

Be forewarned: This question seems way more obvious than it actually is. I'd like to write a template that can accept any concrete class or template class as a template parameter. This might seem useless because without knowing whether the passed in T is templated or not you won't know how to use it. The reason I want this is so that I ...

Generic interface overloading for methods?

Is there a good, generic, way to do the following without resorting to a second method or lots of casts - I want to keep the API as light as possible and it seems ok to me OO-wise: class Foo { public T Bar<T>() where T: IAlpha { /* blahblahblah */ } public T Bar<T>() where T: IBeta { /* blahblahblah */ } } interfac...