overloading

Overloaded operator is never called in C++

I'm writing a math library as a practical exercise. I've run into some problems when overloading the = operator. When I debuged it, I noticed that the call to vertex1 = vertex2 calls the copy constructor instead. In the header file I have: //constructors vector3(); vector3( vector3 &v ); vector3(float ix, float iy, float iz); //opera...

Overloading of Math.sqrt : overloading method seems to hide the original one

Hello, trying to overload the java.lang.Math.sqrt static method for int type : import static java.lang.Math.sqrt; class Test { private static double sqrt(int n) { return sqrt(1.0 * n); } public static void main(String[] args) { System.out.println(sqrt(1)); } } an odd error arises : Test.java...

Overloading embedded Python functions using PyArg_ParseTuple.

If I'm trying to overload an embedded Python function so that the second argument can be a long or an Object, is there a standard way to do it? Is this it? What I'm trying now (names changed to protect the innocent): bool UseLongVar2 = true; if (!PyArg_ParseTuple(args, "ll:foo", &LongVar1, &LongVar2)) { PyErr_Clear(); ...

How do input field methods (text_area, text_field, etc.) get attribute values from a record within a form_for block?

I have a standard Rails 2.3.5 app with a model called Post. Post has an attribute called url, and the following getter is defined: def url p = 'http://' u = self[:url] u.starts_with?(p) ? u : "#{p}#{u}" end If I load up script/console, I can do Post.first.url and get the desired result (e.g. it returns http://foo.com if the attr...

SCJP question: Java method overloading with var-args. What is the rationale?

Why does the following program throw an exception? public class MainClass{ public static void main(String[] argv){ callMethod(2); } public static void callMethod(Integer... i){ System.out.println("Wrapper"); } public static void callMethod(int... i){ System.out.println("Primitive"); } } The method cal...

How do you declare and use an overloaded pool operator delete?

I would like to know how to adapt section 11.14 of the C++-FAQ-lite to arrays. Basically, I would want something like this: class Pool { public: void* allocate(size_t size) {...} void deallocate(void* p, size_t size) {...} }; void* operator new[](size_t size, Pool& pool) { return pool.allocate(size); } void operator delete[](void*...

How to refactor overloaded methods

I have the methods: public MyReturnType MyMethod(Class1 arg) { //implementation } public MyReturnType MyMethod(Class2 arg) { //implementation } //... public MyReturnType MyMethod(ClassN arg) { //implementation } decimal, string, DateTime in [Class1, ... , ClassN] and one common method: public MyReturnType MyMethod(object obj) {...

Inheritance with CRTP

I have these 3 classes. class A { public: virtual void Func() = 0; }; template<class T> class B : public A { public: void Func() { cout << "In B" << endl; static_cast<T*>(this)->Func(); } }; class C : public B<C> { public: voi...

allow using vector functions except one ??

i'm lost in this , i have a class that has three vector objects , class A { vector<int> h1; vector<double> h2; vector <int> h3; } i want to have (inherit ) all the vector functions ( push , size etc ) but EXCEPT " erase " function at first i made the objects public but then erase was available , i donno how inheritance...

Typedef compilation error on function overload

Why can't I compile the program 1 when the the program 2 is working fine ? Why is it's behavior different? Program 1: #include <iostream> typedef int s1; typedef int s2; void print(s1 a){ std::cout << "s1\n"; } void print(s2 a){ std::cout << "s2\n"; } int main() { s1 a; s2 b; print(a); print(b); ...

Overload constructor for Scala's Case Classes?

In Scala 2.8 is there a way to overload constructors of a case class? If yes, please put a snippet to explain, if not, please explain why? ...

Automatic type conversion in Java?

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) { /* ... */...

Overload with different return type in java?

So, I am just starting Java and, even though I have looked in some question about it here at stackoverflow.com and elsewhere, haven't been able to find a straightforward answer to why isn't possible to overload a function just by changing the return type. Why is it so? Will that provably change in a future version of Java? By the way, j...

Can we overload a function based on only whether a parameter is a value or a reference?

I got the answer NO! Because passing by value and passing by reference looks identical to the caller. However, the code below compiles right class A { public: void f(int i) {} void f(int& i) {} }; But when I try to use it, there is compile error. int main () { A a; int i = 9; int& j = i; a.f(1); a.f(i); a.f(...

bug with varargs and overloading?

There seems to be a bug in the Java varargs implementation. Java can't distinguish the appropriate type when a method is overloaded with different types of vararg parameters. It gives me an error The method ... is ambiguous for the type ... Consider the following code: public class Test { public static void main(String[] args) thr...

How do I create a dynamic method in PHP?

I'm trying to extend my ActiveRecord class with some dynamic methods. I would like to be able to run this from my controller $user = User::find_by_username(param); $user = User::find_by_email(param); I've read a little about overloading and think that's the key. I'v got a static $_attributes in my AR class and I get the table name by ...

Override methods in an Objective C Class

Hi, Why can't I do this, and how could I perform the same behavior in Objective C ? @interface Test { } - (void)test:(Foo *)fooBar; - (void)test:(Bar *)fooBar; @end Thanks in advance ! ...

C# overloading with generics: bug or feature?

Let's have a following simplified example: void Foo<T>(IEnumerable<T> collection, params T[] items) { // ... } void Foo<C, T>(C collection, T item) where C : ICollection<T> { // ... } void Main() { Foo((IEnumerable<int>)new[] { 1 }, 2); } Compiler says: The type 'System.Collections.Generic.IEnumerable' cannot be...

Overload and hide methods in Java

Hi, i have an abstract class BaseClass with a public insert() method: public abstract class BaseClass { public void insert(Object object) { // Do something } } which is extended by many other classes. For some of those classes, however, the insert() method must have additional parameters, so that they instead of overriding it I...

Templates, and C++ operator for logic: B contained by set A

In C++, I'm looking to implement an operator for selecting items in a list (of type B) based upon B being contained entirely within A. In the book "the logical design of digital computers" by Montgomery Phister jr (published 1958), p54, it says: F11 = A + ~B has two interesting and useful associations, neither of them having much to do...