FAQ Database Discussion Community


Providing new version of existing macro with parameters

c++,macros,overloading
We have an existing log macro in our code. Lets call it LOG_XXX where XXX is the log level to use, such as LOG_INFO, LOG_DEBUG, LOG_ERROR etc. We stream log messages to the macro: LOG_INFO << "This is a log message."; Under the covers, the macro creates a temporary object...

C++ function overload priority [duplicate]

c++,overloading
This question already has an answer here: Unexpected overload resolution in visual studio involving void*, string and const char[] 3 answers Why would the code give an output: bool ? Is there any way I could make the const char* to match the string version? #include <string> #include <iostream>...

Generic method to perform a map-reduce operation. (Java-8)

java,function,generics,java-8,overloading
How to overload a Function with generic parameter in Java 8? public class Test<T> { List<T> list = new ArrayList<>(); public int sum(Function<T, Integer> function) { return list.stream().map(function).reduce(Integer::sum).get(); } public double sum(Function<T, Double> function) { return list.stream().map(function).reduce(Double::sum).get(); } } Error: java: name clash: sum(java.util.function.Function<T,java.lang.Double>) and sum(java.util.function.Function<T,java.lang.Integer>) have the same erasure...

C++ overload resolution, conversion operators and const

c++,const,overloading,operator-keyword
In this case void f(int *); void f(const int *); ... int i; f(&i); the situation is pretty clear - f(int *) gets called which seems right. However, if I have this (it was done like that by mistake(*) ): class aa { public: operator bool() const; operator char *();...

How can I use different definition of class for different template type in C++? (Class Overloading?)

c++,templates,hashtable,overloading
What I want to do is making a hash table. To make it efficient, I want it to work differently depending on the type of the data. Ex : quadratic probing method for int, separate chaining method for string. I found that I can use typeid() function to compare the...

Dynamic assignment of user defined class in C++, all with the same public function

python,c++,dynamic,overloading
Firstly, I'm a physicist, so my coding experience and knowledge is limited so apologies if I'm not using the correct terminology or if an answer to this question already exists. I've been unable to find anything sufficiently similar that makes sense to me though. Now, I'm performing Monte Carlo simulations...

Best way to “overload” function in python? [duplicate]

python,overloading
This question already has an answer here: Function overloading in Python: Missing [closed] 5 answers I am trying to do something like this in python: def foo(x, y): # do something at position (x, y) def foo(pos): foo(pos.x, pos.y) So I want to call a different version of foo...

c++ va_list function overload

c++,templates,c++11,overloading,va-list
I currently have 2 function overloads: void log(const char* format, ...); void log(const string& message); and I want that in case of this call: log("hello"); the string version will be called, or in other words the first overload should only be called in case of 2 arguments or more. I...

Please explain the default constructor (int = 10)

arrays,class,constructor,operators,overloading
class Array { public: Array(int = 10); // default constructor ~Array(); // destructor protected: int size; // number of elements in the Array int *ptr; // address of dynamically allocated memory }; Could someone explain what the default constructor (int = 10) is about, with an example? Also, how do...

What is the syntax for Operator Overloading in C++?

c++,class,operator-overloading,overloading
I was studying C++, and when I made a little program to learn more about Operator Overloading the program gives an erro in the main function where I wrote "Ponto p1(1,5), p2(3,4), Soma;". Can anyone explain me how to use Operator Overloagin correctly? Thank you. PS: The program is in...

Cannot deduce template argument that is a function

c++,templates,c++11,overloading,typetraits
Why cannot F be deduced for proxy()? It should be possible because I am restricting it - only for functions that return an int. #include <utility> #include <iostream> #include <type_traits> using namespace std; int foo(int bar) { cout << "int" << endl; return 2; } float foo(float bar) { cout...

Distingushing between user and non-user types & templates specialization

c++,templates,overloading,c++14,template-specialization
I need to somehow overload class templates for user and non-user types. The problem arises from the point that template class inherits from the template parameter: #include <iostream> #include<string> #include<map> #include<type_traits> #include<string> using namespace std; class Broken_imput{};//exception class class No_such_class{}; class Io_obj; // For MAP to know about this type...

Overloading template by Return Type

c++,templates,overloading,return-type,specialization
Mooing Duck makes a comment here that "One function can't return multiple types. However, you can specialize or delegate to overloads, which works fine." I started thinking about that, and I'm trying to figure out, how is this legal code: template <typename T> T initialize(){ return T(13); } When called...

Overloading inside interface in Java

java,interface,overloading
I need to implements various update methods for Observers in my project, to overload the Object argument with a specific type. So instead of using public void update (Observable o, Object arg0){ if(arg0 instanceof Class1) .... else if (arg0 instanceof Class2) .... or a switch (which is a very 'C'...

Why doesn't Qt's qHash() have an overload for std::shared_ptr?

c++,qt,overloading,shared-ptr,qhash
I just found out, to my surprise, that the following code does not compile out of the box in C++14 using Qt 5.4: QSet<std::shared_ptr<SomeType>> var; The problem is that there is no overload of the qHash() method for std::shared_ptr, or any other smart pointer as far as I can see:...

overloading arithmetic operators c++

c++,operators,overloading
I just started learning classes on C++ and I'm having quite some issues dealing with overloading arithmetic operators. First of all, in my header file I have: #ifndef MONEY_H #define MONEY_H #include <iostream> using namespace std; class Money{ public: Money(int dollars, int cents); Money(int dollars); Money(); int getDollars() const {return...

Overloading functions without rewriting their whole definition

c++,overloading,function-overloading
See the following example: class bar{ private: unsigned _timeout; public: bool foo(unsigned arg); bool foo(unsigned arg, unsigned timeout); }; bool bar::foo(unsigned arg){ /*50 lines of code*/ if (_timeout > 4) //... } bool bar::foo(unsigned arg, unsigned timeout){ /*50 lines of code*/ if (timeout > 4) //... } As you can...

How can I override the member of (->) operator of a base class

c++,qt,inheritance,overloading,operator-keyword
Background: I have Qt generated UI classes with no common ancestor. I am subclassing a class (say, "Door") that uses one of these UI classes, and the derived class ("OakDoor") will use a different UI class, although much of the UI elements will have the same name. Until now I...

c++ operator overloading library class

c++,overloading,operator-keyword
I have a class from Gtk Library that represents a color(GdkColor)....i have written my own interval tree around it... Basically, it associates different colors to different intervals... Now,in my test case, once i do a query for interval, i want to compare the output with the expected value. typedef struct...

Java runtime polymorphism not working with different parameter types

java,polymorphism,overloading,overriding
I have 3 different class, public class Parent { public void add(double a, double b) { System.out.println("Parent class add(double double)"); } public class Child extends Parent { public void add(long a, long b) { System.out.println("Child class add(long long)"); } } public class Test { public static void main(String arr[]) {...

implementing addition operator for std::vector with type checking

c++,overloading,operator-keyword,stdvector
This is my implementation of + operator for std::vector<T>. //+ operator overloading template <typename T> std::vector<T> operator+(std::vector<T> v1, const std::vector<T>& v2) { if(v1.size() != v2.size()) { throw std::exception("Two vector size must be same"); } for(unsigned int i = 0; i<v1.size(); i++) { v1[i] += v2[i]; } return v1; } it...

Inheriting constructor and providing new overload: no arguments base constructor seems to not participate in overload resolution [duplicate]

c++,overloading,c++14,inheriting-constructors
This question already has an answer here: C++11 constructor inheritance and constructors with no parameters 2 answers Tests show a strange behavior (c++14, g++ 4.9.1, clang 3.5.5): To sum it up: if B provides no other constructors it can use A::A() if B provides other constructors it cannot use...

overloading virtual function and calling derived function by pointer to base class

c++,inheritance,overloading
#include <bits/stdc++.h> using namespace std; class Base { public: virtual int function() const { cout << "Base::function()\n"; return 1; } virtual void function(string) const {} }; class Derived : public Base { public: // overloading function() of base int function(int) const { cout << "Derived::function()\n"; return 4; } }; int...

Precedence while overloading operators C++

c++,operators,overloading,precedence
I have a class, in which I have overloaded some operators. My code is like this: myClass << 12 << 14 << 21 << 44 This works well, firstly runs myClass << 12, then myClass << 14. But I have a problem if I try to do something like this:...

concept of overloading in C++

c++,function,oop,overloading
case 1: you can overload two functions namely: void foo(int *); void foo(const int *); while in , case 2: you can not overload two functions: void foo(int); void foo(const int); I have coded and checked this concept and yet unable to find out the reason to this variation in...

How does Dart's operator overloading translate to javascript?

javascript,dart,operator-overloading,operators,overloading
Very happy to see Dart support operator overloading (very cool :) ), but I am curious as to how this implementation translates to Javascript before I dive into developing something around this? Know this isn't something Javascript supports natively so wondered if there would be any complications around conversion to...

Can't understand this Javascript function (function overloading)

javascript,function,methods,overloading
I'm reading Secrets of Javascript Ninja and came across an example that I cannot fully understand. This same example was cited by some other user here before, but their doubts are different than mine. Here is the example: function addMethod(object, name, fn) { var old = object[name]; object[name] = function(){...

Is it bad practice to overload a function to take both a pointer or a reference?

c++,pointers,reference,overloading
For example, if I had a struct FooSpec: struct FooSpec { /* lots of data */ }; Is it bad practice to provide both of the following as overloaded functions? Or should I just choose one and stick with it? Foo *createFoo(const FooSpec *spec) { /* ... */ } Foo...

Understanding an overloaded operator[] example

c++,operator-overloading,operators,overloading,binary-operators
I am confused with a question that I saw in a c++ test. The code is here: #include <iostream> using namespace std; class Int { public: int v; Int(int a) { v = a; } Int &operator[](int x) { v+=x; return *this; } }; ostream &operator<< (ostream &o, Int...

Using float gives call to overloadrd function is ambiguous error

c++,overloading,ambiguous
My program is below. I'm overloading the function add(), but when I used float datatype it is showing an error and when I change it to double then it's working fine. Why is float causing the error? Errors: In function 'int main()': [Error] call of overloaded 'add(double)' is ambiguous [Note]...

Assignment operator linked list c++

c++,linked-list,overloading,assignment-operator
I am attempting to code an assignment operator for a linked list class in c++. The errors I'm getting say that "head" is undeclared, but I'm not sure where I'm supposed to declare it. It's being used in other functions without a problem. Another error says my use of "this"...

Overloaded final function in derived class [duplicate]

c++,c++11,polymorphism,overloading,final
This question already has an answer here: Why does an overridden function in the derived class hide other overloads of the base class? 3 answers How can I use final overloaded function from derived class? Compiler says 'no matching function for call to 'B::foo()''. class A { public: virtual...

c++ overloading constructor with new type/class and friend

c++,constructor,overloading,friend
Is there anyway to use friend to overload an already declared struct's constructor with one that uses newly defined structure. For example, in the simply example below, I want to overload struct A constructor with a one using struct B, BUT without modifying A.h. main.cpp #include "A.h" #include "B.h" int...

No instance of overloaded function when passing single or vector of objects

c++,function,templates,object,overloading
I'm trying to pass an object and a vector of class objects or two objects to two overloaded template functions named checkPosition. No matter what I try, changing my function to passing by value / reference / pointer / const / not const, it gives me the error; Error: no...

How properly overload size() method/operator in Matlab

matlab,size,overloading,operator-keyword
I have some class with the property val classdef SomeClass < handle properties val; end methods function sz = size(this,varargin) sz = builtin('size',this.val,varargin{:}); end; end end supposedly it should be the proper way to overload this method to get the proper sizes of object of class SomeClass if it is...

Error when using r and l value constructors in a template class

c++,templates,c++11,constructor,overloading
I have a class template like this: template <typename T> class MyClass { public: MyClass(const T & val); // First MyClass(T&& val); // Second }; Basically I want MyClass to be constructible from a T whether it's an rvalue or an lvalue. Now when I have something like const A&...

Overloading operator on member enum of class template

c++,templates,overloading
I'd like to make a template overload of operator | for an enum inside a class template. Here is a minimal example: #include <iostream> using namespace std; template <class T> struct test1 { enum test2 { test3, test4 }; }; template <class T> typename test1<T>::test2 operator | (typename test1<T>::test2 f1,...

Is a templated and a nontemplated version of the same function considered an overload?

c++,templates,overloading,template-specialization
A very formal question: is this considered an overload? Is removing the template fundamentally different than only overloading on arguments? template<class T> void myFunction(const T& t) {} void myFunction(const double& t) {} Then the follow up question, is it better to follow this approach or to use template specialization, instead...

Overloading * multiplication operator twice as a member function?

c++,overloading
Scroll down for TL:DR. This question is similar to this one, but has some differences. It concerns overloading the * operator twice for a class called jVector, which simply represents a two-dimensional cartesian vector. The first type of multiplication is jVector * jVector, or a dot product. The second type...

How do I call a specific overridden method from an inherited method in Java?

java,inheritance,parameters,override,overloading
I have a class named LotteryTicket that have 3 subclasses: Pick4, Pick5, and Pick6. I want to be able to call a method public void pickNumbers()where once called, will be able to recognize which LotteryTicket subclass is being used and ask for the appropriate amount of arguments (i.e. calling pickNumbers()...

C++ stream extraction operator overloading

c++,overloading,operator-keyword
This is a question as to the philosophy (canonical design) of user-written C++ input stream extraction operators (>>). Assume that on entry to the >> operator implementation (for a user-written class), the eof flag is already set for the input stream. Should a user-written extraction operator (>>) set the failure...

Method Overloading with out and optional parameter

c#,overloading
why overloading like this is working in c#? public string DisplayOverload(string a, string b, string c = "c") { return a + b + c; } public string DisplayOverload(string a, string b, out string c) { c = a + b; return a + b; } while this is not...

Why does the Linux C API 'open' support function overloading? [duplicate]

c,linux,api,overloading
This question already has an answer here: Does C support overloading? 9 answers According to the Linux manual page, the Linux C API open has two prototypes as follows: int open(const char *pathname, int oflags); int open(const char *pathname, int oflags, mode_t mode); What makes me confused is: Why...

Injecting a function into a subclass

c++,overloading
Is it possible to do such things in C++14. I have a base class as follows: #include <iostream> class AbstractElement; class ConcreteElement; class SuperConcreteElement; class B { public: void bar(AbstractElement*) { std::cout << "Abstract element" << std::endl; } void bar(ConcreteElement*) { std::cout << "Concrete element" << std::endl; } void bar(SuperConcreteElement*)...

c++ friend overloading operator <<

c++,overloading,friend
I am trying overload the operator << but i keep having this error. I try doing research but with no result. I have a Point2D.h and a Point2D.cpp with a friend functions to overload. Below are my codes: Point2D.h #include <string> #include <iomanip> using namespace std; #ifndef Point2D_H #define Point2D_H...

How to implement abstract method with different number of parameters

java,inheritance,polymorphism,overloading,overriding
I have four classes, one is an abstract class and two are derived classes. in abstract class I have the method edit, which holds 4 parameters I want my derived classes to have the same method but with different number of parameters. The code for abstract class Animal public abstract...

Override of the same function with inherited class

c++,override,overloading
I have this simple code: #include <iostream> using namespace std; class GenericMove {}; class SwapMove: public GenericMove {}; class SoftConstraint { public: virtual int evaluate(GenericMove& gm) = 0; }; class M1: public SoftConstraint { public: int evaluate(GenericMove& gm){ return 1; } int evaluate(SwapMove& gm){ return 2; } }; int main...

Method oveloading - Object vs Object vararg

java,overloading,varargs
See the code below: // 1st method private static void method(Object o){ System.out.println("object method"); } // 2nd method private static void method(Object... o){ System.out.println("object vararg method"); } public static void main(String [] ar){ method(null); // 1st call Integer value=null; method(value); // 2nd call } I expected 1st call and 2nd...

overload resolution fails for derived class

c++,templates,operator-overloading,overloading,overload-resolution
I am using a class mArray, which implements numerical data containers with variable dimensionality template <typename T> class mArray<T>: { ... std::vector<T> my_data; } Another class mImage inherits from mArray and provides image-specific operations, and another class mDcmImage inherits from mImage and provides format-specific functions. I thought this was a...

[solved]std::istream& operator >> and own vector class

c++,class,vector,overloading
i am trying to write a vector class with a method that automaticly creates a vector without given size: std::istream& operator >> ( std::istream& is, Vector& v ) { /* working (but not mine) version i am basing on int size; is >> size; v.create( size ); for( int i=0;...

Updated Method signature is not working in wcf RESTful services

wcf,rest,methods,overloading,fiddler
I have a method like this [WebInvoke(UriTemplate = "StrDetails", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] public List<StrSearchDetails> StrDetails(string str1) { List<StrSearchDetails> details = new List<StrSearchDetails> (); return details; } I am able to hit this method from Fiddler I have updated and replaced like this [WebInvoke(UriTemplate = "StrDetails", ResponseFormat =...

How do I call the first area method from the main method?

java,math,double,overloading,area
I need the area method that contains the formula for the area of a circle to take in the radius that was found by the other area method that calculated the distance...So one of the area methods calculated the distance/radius then I want that answer to be used in the...

Error overloading procedure in Delphi

delphi,override,overloading
For some reason I am now getting this error '[DCC Error] MyFile.pas(52): E2137 Method 'Assign' not found in base class'. Here is a snipit of my Assign procedures that worked fine until I added one more, the last Assign. procedure Assign(MyClass: TMyClass); overload; override; procedure Assign(MyClass: ThisService.MyClass); overload; override; procedure...

Segmentation fault when using a shared_ptr

c++,c++11,design-patterns,overloading,shared-ptr
I am making a particle system and I'm struggling with how to structure my code. The idea is that a user can create one or several ParticleEmitter objects that are passed to a ParticleManager object via the ofxCurlNoise object. Now, I want that when the user updates the ParticleEmitters objects,...

How does C++ resolve specialized templates considering constness, templated-ness, and genericness?

c++,templates,overloading,function-overloading,overload-resolution
I have the following code, which may seem convoluted but comes from real code: #include <iostream> using namespace std; template <class Hrm, class A> void foo(Hrm& h, A& a) { cout << "generic" << endl; } template <template <bool> class Hrg> void foo(Hrg<false>& h, int& a) { cout << "specialized...

What is the basic concept of overloading a method

java,oop,overloading
In overloading when we overload a method why we cant make a new method which works same as overloaded method because we have to write the same number of line of code Such as in my example...why i cant make a new method b() which multiply two numbers. public class...

How Overloading in Java Works? [duplicate]

java,oop,overloading,type-promotion
This question already has an answer here: Overloading methode calles with parameter null [duplicate] 5 answers I fail to understand that why in the following code the output is "String Version". As everything is Derived from Object then why it matches to String version? public class AQuestion { public...

Overload JNI Method

java,jni,overloading
I have an existing JNI method with two parameters. Been around for a while, in use, so I don't want to just change it lest the wrath of angry customers be unleashed. But, I now need to make an adjustment. So, I thought, make a second overloaded method with the...

Fail to compile non-overloaded function in template argument

c++,templates,overloading,derived-class
I have these four classes : class BaseWrapper { public: virtual void Process(int i) = 0; }; template<typename T> class Wrapper : public BaseWrapper { public : Wrapper(T * arg): var(arg) {} void Process(int i) { this->var->Process(i); } private: T * var; }; class Base { public : void Process(int)...

How to extend the following API function in java and not lead to ambiguous

java,api,overloading
I have some existing overloading API: 1). foo(a, b) { return foo(a, b, null); } 2). foo(a, b, c) {return foo(a,b,c,null);} 3). foo(a, b, c, d) { implement of the function...; } Where a, b, c are some data structure the value of which can be null as well. now...

How to Overload functions with multiple parameters? C++

c++,operators,overloading
my question is regarding an HW assignment. I tried google, contacting my teacher (never responded), with no solution. Basically I need to overload a function that has two parameters in a header file. Its very similar to this -> Operator(s) too many parameters for this function? but for some reason...

How do I initialize a custom class like this?

c#,arrays,operators,overloading
C#: How do I initialize a custom class as shown below? Point3 Last = { 0, 0, 0 }; I realize that I can overload the assignment operator '=,' but I want to be able to assign things like this: Point3 p1 = { 3, 4, 5 }; Point3 p2...

Haskell ad hoc polymorphism

haskell,polymorphism,overloading,typeclass
I'm trying to get my head around ad-hoc polymorphism in haskell, that is having the same function provide different behaviour for different argument types. But while the following test code compiles {-# LANGUAGE MultiParamTypeClasses #-} class MyClass a b where foo :: a -> b instance MyClass Bool Int where...

PHP5 Class Property Overloading w/ Dependency Container

php,oop,reference,dependencies,overloading
----------------------UPDATE---------------------------------------------------- I didn't explain very well what I am ultimately trying to accomplish with this overloading. I basically just want to use the config object to store everything and only have one instance spawned of the database, also in config. Then when I load the page, I want to bootstrap...

Passing parent as child reference as an argument to a function that accepts the children as argument in Java

java,inheritance,overloading
Maybe the title is uncorrect, so I'll explain more precisely what I need to do. I have rewritten an "Observer-Observable" interface to let my Observer do an overloading of the method update(Observable o, Object arg) based on various notified Events that I wrote (I'm not using the ones in java.awt)....

Python how to extend `str` and overload its constructor? [duplicate]

python,string,class,python-2.7,overloading
This question already has an answer here: inheritance from str or int 5 answers I have an sequence of characters, a string if you will, but I want to store metadata about the origin of the string. Additionally I want to provide a simplified constructor. I've tried extending the...

overloaded method call ambiguity with ternary operator

java,overloading,definition,method-overloading
I am creating a simple wrapper class for numbers. Simply put, I want it to display the value 42 verses 42.0; however, it should display the value 1.6180338 as that number. Simply enough. Code private double number; ... @Override public String toString() { return String.valueOf( number == longValue() ? longValue()...

Call function using subtype overload

c++,polymorphism,overloading,function-overloading
Consider the following program class A {}; class B : public A {}; void fun(A v) { std::cout << "A" << std::endl; } void fun(B v) { std::cout << "B" << std::endl; } void call(A v) { fun(v); } int main(int argc, char *argv[]) { A a; B b; call(a);...

Overload c++ operator >> so i can read a vector (vector vector;)

c++,vector,overloading,operator-keyword
i want to know how i can overload the >> operator so i can read a vector or a matrix just doing cin >> vector vector(). Thanks!...

Invokable interfaces with overloads

interface,typescript,overloading
I have multiple types that I want to associate an (external) function with. I've decided to hold them into a behind-the scenes array, along the lines of: var labelGenerators : ILabelGenerator[] = [] where ILabelGenerator is supposed to be a general interface for the different shapes the types can have,...

Scheme define-macro and/or define-syntax

macros,polymorphism,scheme,overloading
I want to create an overloaded Scheme macro for a simple form of polymorphism. That is, a macro smart enough to expand differently when given params of different types, so that (look-up key container) does the "right" thing for different kinds of containers. (define-macro (look-up key container) (cond ((table? container)...

Why is overloading not implemented at runtime?

java,runtime,overloading
Why is overloading generally not implemented at runtime? What are the difficulties/implications in allowing a language to select overloaded implementations at runtime, based on the actual (not declared) parameter type?