Click here to Skip to main content
15,881,380 members
Everything / Overloading

Overloading

overloading

Great Reads

by Scanix
SystemFramework defines interfaces, classes, and types to support a native runtime system with its own garbage collector, delegates, etc. The design of SystemFramework classes is similar to those of the .NET Framework.
by Ralph Varjabedian
Super easy way to overload your functions in JavaScript
by Gaurav_Chhatbar
Object oriented implementation of basic language processing / parsing leveraging LINQ / PRISM / UnityContainer.
by Anders Gustafsson, Cureos
Convenience methods for creating dictionaries with LINQ

Latest Articles

by Shamim Uddin
Operator overloading in C#
by Shivprasad koirala
In this article we will learn can we overload mvc controller action methods.
by Ralph Varjabedian
Super easy way to overload your functions in JavaScript
by Gaurav_Chhatbar
Object oriented implementation of basic language processing / parsing leveraging LINQ / PRISM / UnityContainer.

All Articles

Sort by Score

Overloading 

24 Nov 2011 by Scanix
SystemFramework defines interfaces, classes, and types to support a native runtime system with its own garbage collector, delegates, etc. The design of SystemFramework classes is similar to those of the .NET Framework.
10 Aug 2012 by fjdiewornncalwe
As Wes has pointed out in his comment, the definition of your method is incorrect. If you are responding to a click event on a LinkButton as you are in this case, then the method must have a definition that matches protected void MethodName(object sender, EventArgs e). Yours is using...
10 Aug 2012 by Wendelius
The System.EventHandler delegate expects you to have a method with a specific signature present. See: EventHandler Delegate[^]So your code should probably be like:protected void lnkCustomer_Click(object sender, System.EventArgs e) { ... }Addition:Casting the sender...
28 Nov 2013 by Ralph Varjabedian
Super easy way to overload your functions in JavaScript
11 May 2015 by Sascha Lefèvre
You can't have methods in the same class that only differ by their return type.I can think of two ways for code-reuse here, both requiring making Execute(..) a generic method.1) If you can replace Func1 and Func2 by a generic Func :public T Execute(object parameters){ ...
23 Jul 2010 by Emilio Garavaglia
The parameter of anoterclass::GetThing is Blah& that is not const.Hence b.GetThing() resolve to the non-const function, that exist, but it is private.Making a function private makes it not accessible, but doesn't remove it from the declaration scope.tryvoid...
13 Apr 2015 by Richard Deeming
They certainly won't produce the same IL - the overloads version will generate four methods, and the optional parameters version will only generate one.With the overloads version, it's easier to change the default value of a parameter. With optional parameters, the default value is "baked...
8 Feb 2016 by nv3
If that is the exact wording of the question then it's a trap. C++ does not allow to override the behavior of arithmetic operators between predefined types, for example int and char*. That's what the error message C2803 is telling you, and the compiler is correct in this case.So what can you...
29 Jan 2015 by Fredrik Bornander
In these cases;MsgBox(obj.Test(i, 10)) '
13 Apr 2015 by Sergey Alexandrovich Kryukov
Historically, option parameters weren't introduced. In later versions of C#, this feature have been added. That said, optional parameters have additional value for developers. I think this value is quite obvious: you have less methods; the whole thing is somewhat easier for maintenance; you have...
11 May 2015 by BillWoodruff
Here's an example of a static generic Class that will return instances of new Classes where the internal data type used by the new Class is constrained to be a struct. I believe this example demonstrates the "usual" features of implementing the Factory Class pattern in C#: use of an interface;...
14 Nov 2015 by Richard MacCutchan
See https://www.google.com/search?q=operator%20overloading%20c%2B%2B[^].
18 Feb 2018 by Maciej Los
Assuming that a, b and c are lengths of sides of triangle, you can't create a triangle from 1, 2, 3! Check this: How to Determine if Three Side Lengths Are a Triangle: 6 Steps[^] In other words: TriangleIsPossible = (a+b>c && a+c>b && b+c>a)
22 Mar 2011 by mbue
Im a little bit confused with your naming. What should i say it works as expected.class MyBaseClass{};class MyDerivedClass : public MyBaseClass{};class MyBaseAssigneeClass{public: //base class assignment operator: virtual MyBaseAssigneeClass& operator = (const MyBaseClass&...
24 Mar 2011 by Olivier Levrey
Try to define the = operator in the base class only and use an overridable method:class MyBaseClass{public: MyBaseClass& operator = (const MyBaseClass& that) { return Assigns(that); }protected: virtual MyBaseClass& Assigns(const MyBaseClass& that) { ...
9 Mar 2012 by El_Codero
Hi Peter,your goal should be easy to achieve, but I think you mixed a few things. Hope my code sample below is doing what you what you want to achieve.It simply returns a Boolean "false" if "input"-value is
4 Oct 2012 by NexusNemesis
Which cannot be overloaded here?a] ( . )b] ( :: )c] ( ?: )d] ( += )e] ( >> )f] (
4 Oct 2012 by JF2015
a, b, c cannot be overloaded. See here:http://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading[^]
19 Jul 2013 by Gaurav_Chhatbar
Object oriented implementation of basic language processing / parsing leveraging LINQ / PRISM / UnityContainer.
20 Mar 2014 by Divakar Raj M
I want to call a method on two separate occasions. The two occasions pass two different types parameters, they differ by datatype. How do i achieve this without overloading.This is my codeusing System;using System.Collections.Generic;using System.Linq;using...
20 Mar 2014 by Kornfeld Eliyahu Peter
Read here about variable data type in C# (it's there since Visual Studio 2008/C# 3.0)...http://msdn.microsoft.com/en-us/library/bb383973(v=vs.120).aspx[^]
20 Mar 2014 by Matt T Heffron
Here's a start: public static void Hit(IList a) { Console.WriteLine(a[0]); }Both of the types of Lists you're using implement the IList interface.Without seeing what else you are doing I can't be sure, but using generics[^] looks like it may be a...
20 Mar 2014 by BillWoodruff
The easy way:private void TestHit(){ var intlist = new List {2, 3, 7}; var stringlist = new List {"A", "B", "C"}; Hit(intlist); Hit(stringlist);}private void Hit(List theList){ if (theList == null) throw new...
29 Jan 2015 by Dave Kreskowiak
This really has more to do with VB.NET doing implicit conversions than it does with overloading.The thing is that VB.NET will implicitly upconvert a value to the next wider type, but not convert anything down to a narrowing type unless explicitly told to do so. This is because a widening...
13 Apr 2015 by Paulo Augusto Kunzel
Hello,This may not be the most common question, but it's bugging me.Lets say, I have a method that is overloaded a couple times to have all logic in one place like this(dumb example, but it kind covers what I'm saying):public int sum(int x, int y) { return...
13 Apr 2015 by BillWoodruff
In this case I would vote for optional parameters because what you are doing seems "orthogonal" to me: there's no significant variation.You could also consider using Enumerable.Sum:// requires Linq and .NET 3.5public int SumAll(params int[] values){ return values.Sum();}The 'Sum...
6 Feb 2016 by OriginalGriff
I don't think you want to do that: it's very counter-intuitive.Normally, when you add an integer and a pointer, you get a pointer that has moved on within the memory pointed to by the original pointer: char *c = new char[10]; cin >> c; // abcdefg c = c + 2; cout
31 Jan 2017 by KarstenK
It is correct by your code:T2 = ++(++T1);1. you increment the T1 in the braces2. you increment the T1 outside the braces3. you assign the T1 values to T2tip: use a debugger to step into the three function calls of this code line.
26 Apr 2018 by Frederic GIRARDIN
Hi. I'm using vb.net and I'm trying to extend and/or overrides a class from a referencing DLL (called IFCX) in the using DLL (ESTX). Whole shoud asking me "WHY doing this ?" the reason is overwellmind : - Both DLL have a means, but when i use ESTX, i load the data from IFCX too, but IFCX object...
8 Mar 2019 by Richard MacCutchan
The definition of the operator
6 Jul 2021 by OriginalGriff
That code is ... pretty poor. Not only will it not compile as Jeron1 has said, even if it did it couldn't work as the first loop is irrelevant: the values in node_n will always be the last elements in the connectivity array since you overwrite...
23 Jul 2010 by User 583852
I asked this question previously but mistyped the key line of code. I couldn't figure out how to modify it so I deleted it and am reposting it. My apologies if this is not the done thing.class Thing{public: Thing() {}...};class Blah{public: const Thing* GetThing()...
25 Jul 2010 by User 583852
Thanks to those who replied.From the c++ spec...13.3.1.4:For non-static member functions, the type of the implicit object parameter is reference to cv X where X isthe class of which the function is a member and cv is the cv-qualification on the member function declara-tion. [Example: for...
24 Mar 2011 by Jim @ JCT
Thanks for the replies chaps, but gah, I've given the example wrong. Dammit, sorry. Let me try again.class MyBaseClass{ //base class assignment operator: virtual MyBaseClass& operator = (const MyBaseClass& that);};class MyDerivedClass : public MyBaseClass{ ...
10 Oct 2011 by WodgerDodger
I need to create a template (non-member) function with two implementations for a multi-threaded delegate connection system.The chosen implementation needs to be determined by the cv qualifier of the last template parameter which expects a (non-type) member function pointer type.This sounds...
9 Oct 2011 by mbue
Your source pointer type must depend on the target pointer type because the pRetargetingFunction depends on the target pointer type. An explicit cast is not useful for template...
6 Nov 2011 by WodgerDodger
When I overload a class's member function solely on the const qualifier of the parameter (which is a different class's member function pointer), the compiler resolves the overload and performs as expected.When I attempt the same overload for a non-member function, the compiler (VS2010 Express)...
6 Nov 2011 by Andrew Brock
Ignore my comment. I wasn't thinking straight.The issue is that this code is defined in a header file.This means that for every .cpp file that includes this, the function code is defined (under the same name).The #pragma once only stops this file getting included more than once by any...
9 Mar 2012 by PeterC#2012
Hi!Im a beginner at C# programming. Im studying a class in this language and unfortunatly im stuck:(The assignment is about a Cinema system. Userimputs a value-----> if valid it appears on the listbox with person name, ticket price and total revenue for the day.the above i have no...
7 Jun 2012 by JothiMurugeswaran
1. How to use Function Overloading in C# 4.0 interface?2. How to use default parameters in C# 4.0 interface?
4 Oct 2012 by eugene.shikhov
not exactly. take a look at the tutorial:Overloading unary operators +, -, and ![^]Overloading operators using member functions[^]Overloading the increment and decrement operators[^]
9 Jul 2013 by NightCodeStalker
The following code is considered invalid by the compiler:class Foo { void foo(String foo) { ... }}class Bar extends Foo { @Override void foo(Object foo) { ... }}I think that this is described in the JLS 8.4.8.1: "The signature of m1 is a subsignature (§8.4.2) of...
15 Apr 2015 by Philippe Mori
In practice, you just have to use some common senses and answer some simple questions like:Do you want the implementation method to be public?In some cases, it does not make sense to have all parameters at once as some of them might be exclusive.Does the function with less parameters...
14 Nov 2015 by MaxySpark
How can I overload > bitwise operator in c++?
20 Nov 2015 by Member 11633058
Hi I am a beginner in C# & .NET.In the following code CreateCountry function declared as int and its return type is also int (this part I got it)But I am confused about the overloaded string variable in int function, also nowhere in this piece of code the given string is converted to int...
20 Nov 2015 by Dave Kreskowiak
Your question doesn't make a lot of sense.Are you asking how to overload a method with a copy that takes the same parameter type, a string in your case, and differs only by the return type? Are you asking how a method can return either an integer OR a DALCountry type?It's easy. You...
20 Nov 2015 by BillWoodruff
'CreateCountry is not "declared as int." It returns an 'int, and it takes a 'string parameter.The 'CreateCountry method in the 'DALCountry class must be different from the 'CreateCountry method you show here, or: the method would call itself recursively until there was a stack overflow...
6 Feb 2016 by Member 12147362
hello. I have the task to overload operator + between a char* and an integer like this:char* operator+(const int k, char* c){ for (int i = 0; i> c;// abc c = c + 2; cout
6 Mar 2017 by Member 13041778
I am having trouble figuring out the syntax for how i am suppose to Call the member function to copy the values of my array into the appropriate member variables. This is specifically for the overloaded constructor because I think all the other functions are correct but they might not be....
6 Mar 2017 by CPallini
The correct syntax isStudent::Student(int tID, string f, string l, int list[]){ setID(tID); setFName(f); setLName(l); setScores(list);}Assuming Student.h contains something likeclass Student{ int ID; int scores[5]; string firstName, lastName;public: ...
5 Dec 2017 by Optimistic76
Hi everybody, I have a question for you about overloading operator = for derived classes. Suppose for example to have two classes A, B defined like this: class A { protected: double v_; public: A(double initVal = 0.0) : v_(initVal) {} virtual ~A() {} double v() { ...
5 Dec 2017 by tra_la_la_la
Make the constructor B(A& src) explicit. Smth like explicit B(A& src)
5 Dec 2017 by Jochen Arndt
I have not checked your code and what it does because it is not implementing the assignment operators in the usual way. They should not return a copy but the object itself (note also that the operator in your class A is returning nothing). It should be: class A { // ... double v() const...
5 Dec 2017 by CPallini
I suggest you to have a look at this page: Copy constructors, assignment operators, - C++ Articles[^].
5 Dec 2017 by Optimistic76
if you are interested maybe i have found what i was searching for C++ Explained: Object initialization and assignment, lvalues and rvalues, copy and move semantics and the copy-and-swap idiom | Katy's Code[^]
17 Feb 2018 by phil.o
Well, 1 + 2 + 3 e2 is false. Moreover, A triangle is defined by 3 points; each point having two components, you cannot define a triangle with only three scalar values. You need three points, or 6 scalar values. I would add that the entire idea to compare two triangle is...
17 Feb 2018 by OriginalGriff
What defines two triangles as "bigger" and "smaller"? Is it the perimiter? The area? The location? About the only thing we know is that it's not "the sum of the three angles" becuase that is always the same value, given undistorted spacetime. The area of a triangle is 0.5 * b * h where b is...
18 Feb 2018 by OriginalGriff
When you posted this question this morning: C# how to compare two triangles with operator >[^] you were told what was wrong with your whole approach. The problems haven't changed; and neither has your code...
26 Apr 2018 by Frederic GIRARDIN
Imports System.Runtime.CompilerServices Module MyClassExtended _ Public Function Assignations(Sender as IFCX.IMPORT_ELEMENT,about as ESTX.BL_ESTX) as list(of ESTX.ASSIGNATION) dim _list as list(of ESTX.ASSIGNATION) 'can't raise specific event Ask_Assignations because...
8 May 2018 by Member 13817351
Problem: It is instructed that take the value from user in setvalue function then what is the use of overloaded constructor in this program? You are required to implement Employee Management System. There will be an employee class having following attributes: Name(String), Father_Name(String),...
8 May 2018 by KarstenK
Write the Employee class in extra files, separated in header (employee.h) and implemention (employee.cpp) files. It is standard in coding to do this. tip: in the default constructor you MUST also set the values to the member. Like that: Employee():name(""), father_name(""), emp_id(0), bps(0),...
8 May 2018 by Member 13817351
can you review my code to tell me what is error in my code.
16 Jan 2022 by kavinderrana121
I was reading Minimum spanning tree - Kruskal with Disjoint Set Union - Competitive Programming Algorithms[^] and I am confused in below operator overloading part used in above implementation,here how we can sort without defining the comparator. How is sorting taking Place over two...
8 Mar 2019 by CPallini
Richard already gave you the correct answer. As an addendum, looking at the documentation std::sort - cppreference.com[^], note the first overload of std::sort is used. Moreover, reading this article: "Sorting a vector of custom objects in C++"[^], could be helpful.
6 Jul 2021 by Member 15181211
Imagine we want to do some matrix assembly with defining operator overloading. node_0,node_1,and node_2 are coming from the connectivity matrix which is n_element*4. I've already defined the summation operator (matrix+ scalar) and summation...
16 Jan 2022 by Member 15502389
Hello, there is another method to sort vector of edges: struct Edge { int u, v, weight; }; bool comp(Edge e, Edge f) { return e.weight edges; sort(edges.begin(), edges.end(), comp); After you define struct,...
29 Jul 2022 by Shivam Saurabh
Create a class Distance with two data members: feet and inches and functions for input and showing the distance objects. Create an overloaded * operator so that two distances can be multiplied together. Make it in a way so that you can also use...
28 Jul 2022 by OriginalGriff
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for...
29 Jul 2022 by Richard MacCutchan
See 3. Data model — Python 3.10.5 documentation[^] for operator overloading in Python.
29 Oct 2022 by Utkarsh Singh Rajawat
Like let's say - A a1,a2,a3,a4; a1=a2*a3+a1++/2; Assuming there is a class A and *,+,post increment operator(++) and / are overloaded in class A. a1,a2,a3,a4 are objects of A class. So what's the order of evaluation of operators in above...
29 Oct 2022 by OriginalGriff
That's tricky ... Why Does x = ++x + x++ Give Me the Wrong Answer?[^]
29 Oct 2022 by Richard MacCutchan
See also C++ Operator Precedence - cppreference.com[^].
29 Oct 2022 by Greg Utas
Whether they've been overloaded or not, the compiler generates code to execute operators in the same order. If you read the page that Richard linked, I think you'll discover that you're mistaken about the order in which the evaluation occurs for...
17 Mar 2023 by Phoenix Liveon
How do we properly create an overloaded operator specifically this "
17 Mar 2023 by klarrra
Find some good help here. https://kbrown.hashnode.dev/c-in-computer-science
18 Dec 2012 by Anders Gustafsson, Cureos
Convenience methods for creating dictionaries with LINQ
29 Jan 2015 by Sujith Karivelil
I have a class having two functions with same name and different type of arguments as described below: Public Class functionOverload'
26 Nov 2010 by Achilleas Margaritis
C++0x Dynamic Message Passing Ala Objective-C
22 Mar 2011 by Jim @ JCT
Ok, say I have the following classes:class MyBaseClass;class MyDerivedClass : public MyBaseClass;class MyBaseAssigneeClass{ //base class assignment operator: virtual MyBaseAssignableClass& operator = (const MyBaseClass& that);};class MyDerivedAssigneeClass : public...
1 Feb 2017 by Jochen Arndt
Your implementation of the postfix operator is wrong. You must return temp (the unchanged object) instead of the modified object:Time operator++(int){ Time temp(hours, minutes); minutes++; if (minutes >= 60) { hours++; minutes = minutes - 60; } ...
26 Sep 2010 by Andreas Gieriet
.NET 4 finally allows to define polymorphic extension visitors
10 Jul 2013 by Patrick Wanjau
The answer is plain simple, in Java, for method overriding, you must have the exact signature of the super type. However, if you remove the @Override annotation, your method would be overloaded and your code won't break. This is a Java implementation that ensures that you mean the method...
2 Oct 2016 by Shamim Uddin
Operator overloading in C#
4 Dec 2014 by Shivprasad koirala
In this article we will learn can we overload mvc controller action methods.
20 Mar 2014 by Tejas Vaishnav
Hi you can play like this...static void Main(string[] args) { List intlist = new List(); intlist.Add(2); intlist.Add(3); intlist.Add(7); List stringlist = new List(); ...
23 Jul 2010 by Sauro Viti
If you cast b to const Blah& then you can call only methods declared as const, i.e. that cannot modify the internal state of the object. In this case the choice is for const Thing* GetThing() const.But it will continue to work if you write:class Thing{public: Thing()...
8 May 2018 by Rick York
The purpose of the overloaded constructor is to give you two ways to construct the object. You can either have it initialized to default values or you can construct it with specific values.
10 May 2015 by Sri Prasad Tadimalla
How do I reuse code in the following functions to return objects of different types?public A Execute(object parameters){ using (var x = X.Create(parameters)) { Process(x); return Func1(x); }}public B Execute(object parameters){ using (var x =...
1 Feb 2017 by Member 12975649
#include using namespace std;class Time {private: int hours; // 0 to 23 int minutes; // 0 to 59public: // required constructors Time(){ hours = 0; minutes = 0; } Time(int h, int m){ hours = h; minutes = m; } // method to...
20 Nov 2015 by Foothill
It would be better design to check the country argument and throw an exception if it's not right. The calling function would have a try/catch block to handle the error.public DALCountry CreateCountry(string countryName){ if (String.IsNullOrEmpty(countryName)) throw new...
7 Nov 2011 by Stefan_Lang
There is no inherent meaning when you define a non-member function const. In a class, a member function that is declared const means that it won't change the instance of the class you call it on. For a non-member function that makes no sense however! There is no 'instance' of a class that a...
10 Aug 2012 by Member 8759797
need to set the Text in a gridview column to something other than what shows up in gridview with this code:'> Of course what shows up is the contents of "ImgLnk" in...
31 Jan 2017 by Richard MacCutchan
Since you are overriding both operators the normal ordering rules do not apply. The right-hand side of each expression will be completely evaluated before the result is passed to the left-hand side. Stepping through the code with your debugger will show you what happens.
20 Mar 2014 by Divakar Raj M
Solved it..using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace DifferentDatatypeOfMethods{ class Program { static void Main(string[] args) { List intlist = new List(); ...
7 Jun 2012 by RDBurmon
With simple googling I found thishttp://stackoverflow.com/questions/3316402/method-overloading-vs-optional-parameter-in-c-sharp-4-0[^]Hope this helps , If yes then plz accept and vote the answer. Any queries / questions on this are always welcome.Thanks &...
4 Oct 2012 by NexusNemesis
Is this correct?Unary operator overloaded by means of a friend function takes one reference argument.Thanks!
16 Jan 2022 by Member 15502389
And that's already working code for Kruskal algorithm for finding MST (obviously using Disjoint Set Union) struct Edge { int u, v, weight; }; bool comp(Edge e, Edge f) { return e.weight edges; int n, m,...
20 Nov 2015 by Rene Bustos
Hello.you can not see the conversion from String to integer, because it is a class method : DALCountry .DAL , stands for: Data Access Layer .The conversion of " countryname " to integer data type , can be given in different ways, but the most is roughly a query to the database :...
5 Nov 2017 by Member 13483093
output is 505 505 how ? what does super(i*j) does What I have tried: class A { public A(int i) { System.out.println(myMethod(i)); } int myMethod(int i) { return ++i + --i; } } class B extends A { public B(int i, int j) { ...