overloading

Is overloading the only way to have default function arguments in C#?

Is it true that the only way to handle default function arguments is through function overloading? For example, in PHP I can do this: function foo($x, $y=0) { } Would the best way to handle it in C# be this? void foo(int x) { foo(x, 0); } void foo(int x, int y) { } Example lifted from here Edit Made the C# example into actual...

C++ overload resolution

Given the following example, why do I have to explicitly use the statement b->A::DoSomething() rather than just b->DoSomething()? Shouldn't the compiler's overload resolution figure out which method I'm talking about? I'm using Microsoft VS 2005. (Note: using virtual doesn't help in this case.) class A { public: int DoSomething(...

Are C++ non-type parameters to (function) templates ordered?

I am hosting SpiderMonkey in a current project and would like to have template functions generate some of the simple property get/set methods, eg: template <typename TClassImpl, int32 TClassImpl::*mem> JSBool JS_DLL_CALLBACK WriteProp(JSContext* cx, JSObject* obj, jsval id, jsval* vp) { if (TClassImpl* pImpl = (TClassImpl*)::JS_GetInst...

Same property, different types.

Let's say you have a class with a Uri property. Is there any way to get that property to accept both a string value and a Uri? How would you build it? I'd like to be able to do something like one of the following, but neither are supported (using VB, since it lets you specify type in the Set declaration for the 2nd one): Class MyClas...

Default parameters with C++ constructors

Is it good practice to have a class constructor that uses default parameters, or should I use separate overloaded constructors? For example: // Use this... class foo { private: std::string name_; unsigned int age_; public: foo(const std::string& name = "", const unsigned int age = 0) : name_(name), age_(ag...

function overloading fail: why did these operators clash?

Hi, I've got a big big code base that includes two main namespaces: the engine and the application. The engine defines a vector3 class as a typedef of another vector3 class, with equality operators that sit in the engine namespace, not in the vector3 class. I added a class to the application that also had equality operators in the a...

Puzzle: Overload a C++ function according to the return value

We all know that you can overload a function according to the parameters: int mul(int i, int j) { return i*j; } std::string mul(char c, int n) { return std::string(n, c); } Can you overload a function according to the return value? Define a function that returns different things according to how the return value is used: int n = mul(...

Method Overloading. Can you overuse it?

What's better practice when defining several methods that return the same shape of data with different filters? Explicit method names or overloaded methods? For example. If I have some Products and I'm pulling from a database explicit way: public List<Product> GetProduct(int productId) { // return a List } public List<Product> G...

What is the use of const overloading in C++?

In C++, a function's signature depends partly on whether or not it's const. This means that a class can have two member functions with identical signatures except that one is const and the other is not. If you have a class like this, then the compiler will decide which function to call based on the object you call it on: if it's a cons...

Should you declare methods using overloads or optional parameters in C# 4.0?

I was watching Anders' talk about C# 4.0 and sneak preview of C# 5.0, and it got me thinking about when optional parameters are available in C# what is going to be the recommended way to declare methods that do not need all parameters specified? For example something like the FileStream class has about fifteen different constructors whi...

What Does "Overloaded"/"Overload"/"Overloading" Mean?

What does "Overloaded"/"Overload" mean in regards to programming? ...

Function overloading vs. default parameters in VB.NET?

In VB.NET, which is better to use: function overloading or default parameters? ...

When overriding equals in Java, why does it not work to use a parameter other than Object?

I ran into an interesting behavior recently. It seems that if I override .equals() to take a parameter other than Object, it doesn't get called. Can anyone explain to me why this is happening? It seems to violate my understanding of polymorphism in OOP, but maybe I'm missing something. Here's much simpler code that shows what I'm see...

Accessing constructor of an anonymous class

Lets say I have a concrete class Class1 and I am creating an anonymous class out of it. Object a = new Class1(){ void someNewMethod(){ } }; Now is there any way I could overload the constructor of this anonymous class. Like shown below Object a = new Class1(){ void someNewMethod(){ } publ...

Implement Array-like behavior in JavaScript without using Array

Is there any way to create an array-like object in JavaScript, without using the built-in array? I'm specifically concerned with behavior like this: var sup = new Array(5); //sup.length here is 0 sup[0] = 'z3ero'; //sup.length here is 1 sup[1] = 'o3ne'; //sup.length here is 2 sup[4] = 'f3our'; //sup.length here is 5 The partic...

How to link / group overloads in C# XML comments?

In XML documentaiton comments for C#, is there a way to mark two or more functions to be overloads of each other, so that they reference each other automatically? Ideally, they'd also be grouped in the sandcastle-generated documentation somehow. Purpose: Often, I want to link to this group of functions, e.g. in a list of utility functio...

Passing superclass as parameter to method expecting sub class

Hello, I have an object tree that looks something like Ball / \ LegalBall IllegalBall And I have 2 methods: class o { AddBall(LegalBall l) AddBall(IllegalBall i) } in another class I'd like to do the following: o.AddBall(myBall); where myBall is of type Ball. And get it to call the correct method de...

Which compiler is correct for the following overloading/specialization behavior?

Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T *, int) { puts("T"); } template <typename T> struct foo_fun { static void fun() { foo((T *)0, 0); }; }; } namespace Foo { void foo(int *, int) { puts("int"); } } using namespace Foo; int main() { foo_fun<int> fun; fu...

How can I overload a C# method by specific instances of a generic type.

Coming from a C++ background, I've run into a snag with overloading based on a specific instance of a generic type. The following doesn't work since only once instance of the code for the Foo<T> class is ever generated, so inside the Method, the type of this is simply Foo<T>, not Foo<A> or Foo<B> as I'd hoped. In C++ I'm used to templa...

Overriding an operator using const for both parameters in C++

Hi, I'm trying to create an overridden operator function using both const parameters, but I can't figure out how to do it. Here is a simple example: class Number { Number() { value = 1; }; inline Number operator + (const Number& n) { Number result; result.value = value + n.value; re...