473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Something about overloading really bugs me

As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.
This just seems like a cruddy workaround... to have most everything as part
of your one overloaded function, but then to have 1 or 2 stragglers that
have to be named separately.

Is this just life and I have to deal with it, or is there a better approach?

Thanks,
Ron

Dec 3 '07 #1
8 990
On Dec 3, 12:54 pm, "Ronald S. Cook" <rc...@westinis.comwrote:
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.

This just seems like a cruddy workaround... to have most everything as part
of your one overloaded function, but then to have 1 or 2 stragglers that
have to be named separately.

Is this just life and I have to deal with it, or is there a better approach?

Thanks,
Ron
If that sort of overloading were allowed, how would the compiler
choose which method to call? It has no idea what the string you pass
in is, so there's no way it could call the correct method.

Chris
Dec 3 '07 #2
On Dec 3, 12:54 pm, "Ronald S. Cook" wrote:
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.

This just seems like a cruddy workaround... to have most everything as part
of your one overloaded function, but then to have 1 or 2 stragglers that
have to be named separately.

Is this just life and I have to deal with it, or is there a better approach?

Thanks,
Ron
Besides bad example EmployeeNumber would probably be an integer.
Faced with this same problem I created an enum containing firstname
and lastname and based off of what name it is, passed it to the
correct database function.
Dec 3 '07 #3
"Chris Dunaway" <du******@gmail.comwrote in message
news:48**********************************@w40g2000 hsb.googlegroups.com...
On Dec 3, 12:54 pm, "Ronald S. Cook" <rc...@westinis.comwrote:
>As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.

This just seems like a cruddy workaround... to have most everything as
part
of your one overloaded function, but then to have 1 or 2 stragglers that
have to be named separately.

Is this just life and I have to deal with it, or is there a better
approach?

Thanks,
Ron

If that sort of overloading were allowed, how would the compiler
choose which method to call? It has no idea what the string you pass
in is, so there's no way it could call the correct method.
Wouldnt it be good if the compiler just could simply read our minds :D

in the above example you could have a string to see if it was a name or a
number.
assuming no employee is called 4 or something

Colin =^.^=
Dec 3 '07 #4

As mentioned, how would the compiler know which one to call when you asked
for

myobject.GetEmployeeList("Harrison");
Do you want
Harrison Ford
or
John Harrison
Mary Harrison
???
That's magic fairy dust stuff, where you want to computer to read your
mind....which it cannot do.

...

If it bothers you , you can do a simple enum

public enum EmployeeSearchType
{
ByLastName ,
ByFirstName
}
GetEmployeeList ( EmployeeSearchType est , string name )
{

}

myobject.GetEmployeeList(EmployeeSearchType.ByFirs tName , "Harrison");
//I get Harrison Ford
myobject.GetEmployeeList(EmployeeSearchType.ByLast Name , "Harrison");
//I get John Harrison and Mary Harrison



"Ronald S. Cook" <rc***@westinis.comwrote in message
news:B8**********************************@microsof t.com...
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.
This just seems like a cruddy workaround... to have most everything as
part of your one overloaded function, but then to have 1 or 2 stragglers
that have to be named separately.

Is this just life and I have to deal with it, or is there a better
approach?

Thanks,
Ron

Dec 3 '07 #5
On Dec 3, 1:54 pm, "Ronald S. Cook" <rc...@westinis.comwrote:
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.

This just seems like a cruddy workaround... to have most everything as part
of your one overloaded function, but then to have 1 or 2 stragglers that
have to be named separately.

Is this just life and I have to deal with it, or is there a better approach?
I've never come across that deficiency but I can see your point. The
way I would handle it would be to make one overload that took two
parameters, one the string, and the second one being some kind of
indicator variable (bool or enum) that indicated which of the two sub-
functions was being invoked. Then, in the shared constructor, handle
both situations, using the second parameter to decide which of the two
functions was being invoked.
Dec 3 '07 #6
Liz

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:B8**********************************@microsof t.com...
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.
This just seems like a cruddy workaround... to have most everything as
part of your one overloaded function, but then to have 1 or 2 stragglers
that have to be named separately.

are you sure you need overloads at all?

GetEmployeeList(string s, int qtype)
{
if (qtype == 1)
sql = "SELECT * FROM emps WHERE empNo = " + s;
else
sql = "SELECT * FROM emps WHERE empLastName = " + s;
}

IF you're after something of that sort, it's probably a little cleaner than
overloading GetEmployeeList anyway ...

Dec 3 '07 #7

"Ronald S. Cook" <rc***@westinis.comwrote in message
news:B8**********************************@microsof t.com...
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.
That's not what overloads are for.

Overloads are for default arguments mainly, sometimes also for handling the
same data represented with a different type.

All overloads should perform the same operation. In your example, there are
two different operations:

"Find the employee with the given employee number"
"Find all employees with the given last name"
>

This just seems like a cruddy workaround... to have most everything as
part of your one overloaded function, but then to have 1 or 2 stragglers
that have to be named separately.

Is this just life and I have to deal with it, or is there a better
approach?
The better approach is to not use overloads at all in this scenarion, then
everything is consistent.
>
Thanks,
Ron

Dec 3 '07 #8
You can use user defined types for your two string pipes by putting them
in objects, such as by making a class or struct.
Here is an example of both:

public class enumb
{
public string enumber;
public enumb(string st) {enumber=st;}
}
public struct enam { public string elstnam;}

public static void getemployeelist (enumb e)
{Console.WriteLine(e.enumber);}
public static void getemployeelist( enam e ) {
Console.WriteLine(e.elstnam); }

static void Main(string[] args)
{
enumb e1=new enumb("42");
enam e2= new enam();
e2.elstnam = "Smith";
getemployeelist(e1);
getemployeelist(e2);
}

Dec 3 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: | last post by:
The code below I suspect creates memory leaks: In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object. After "c += 'new_object'" we have an unreferenced object which...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: tttpppggg | last post by:
Hi. I am just sharpening up some code, getting operators going, but have bumped into a hurdle around applying operators to the following line of code: s1 = s1 + s1; where s1 is a class...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
10
by: chetan | last post by:
Hello All ! In c++, are basic data type int,char,long,..,etc. classes ? And if so, How can i overload new and delete operators for this this objects ? Thanks in advance ! Chetan
18
by: uday | last post by:
Does C supports overloading. I am thinking no. But some people are saying yes. If yes how. Please answer with examples. Also in c++ size of empty class is one. why. and what are the default...
1
by: Chris Austin | last post by:
A co-worker asked me today what would happen if Session.Add() method was called multiple times with the same key. My first thought was that it would throw some sort of duplicate key exception. ...
3
by: Chameleon | last post by:
What is better if you want upcasting in intermediate classes like below? Multiple Inheritance and Overloading or simply RTTI? RTTI wants time but MI and Overloading create big objects (because of...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.