Hi all,
I'm trying to do something with method overloading and I can't seem to
get it to work
my code is along the lines of
public int method1(int a, int b, int c){
//method body
}
public int method1(int a, int b){
method1(a,b,nul l}
public int method1(int a){
method1(a,null, null)
}
I understand that int is a value type and so can't be null, but all
the examples i find using value types for overloading pass 0, which is
no good for me as this is a meaningful value for the parameter, i want
to pass something that says the parameter is empty. I have tried using
Int32 and this complains that it can't be null too.
I have considered creating my own object wrapping int just so i can
pass null, but that seems absurd. Is there an object (equivalent to
Java's Integer) that i have missed, or are you supposed to do
overloading in some different way in this situation?
many thanks
James 6 1648
Value types simply can't be null. Your choices would be:
1) Use a "magic number" to signifiy empty, such as Int32.MinValue.
2) Make the 2nd & 3rd arguments object, and unbox the int if not null
3) Use SqlInt32 instead of int. Then you can test for, say, b.Value ==
b.Null. This is similar to what you were thinking of doing -- wrapping the
integer in a class. It's already been done for you.
--Bob
"james b" <j@mesbridger.c om> wrote in message
news:be******** *************** ***@posting.goo gle.com... Hi all, I'm trying to do something with method overloading and I can't seem to get it to work
my code is along the lines of public int method1(int a, int b, int c){ //method body } public int method1(int a, int b){ method1(a,b,nul l} public int method1(int a){ method1(a,null, null) }
I understand that int is a value type and so can't be null, but all the examples i find using value types for overloading pass 0, which is no good for me as this is a meaningful value for the parameter, i want to pass something that says the parameter is empty. I have tried using Int32 and this complains that it can't be null too.
I have considered creating my own object wrapping int just so i can pass null, but that seems absurd. Is there an object (equivalent to Java's Integer) that i have missed, or are you supposed to do overloading in some different way in this situation?
many thanks
James
James,
I would consider a variation of Bob Grommes's second item: 2) Make the 2nd & 3rd arguments object, and unbox the int if not null
Have a fourth helper function that the three overloaded functions call. This
helper function has extra parameters to identify whether b & c are null or
not.
public int method1(int a, int b, int c)
{
method1(a, b, false, c, false);
}
public int method1(int a, int b)
{
method1(a,b,fal se, 0, true);
}
public int method1(int a)
{
method1(a, 0, true, 0, true);
}
private int method1(int a, int b, bool bNull, int c, bool cNull)
{ //method body
}
The helper function is private as it is not intended to be called outside
the function. This avoids boxing & unboxing of the parameters...
Hope this helps
Jay
"james b" <j@mesbridger.c om> wrote in message
news:be******** *************** ***@posting.goo gle.com... Hi all, I'm trying to do something with method overloading and I can't seem to get it to work
my code is along the lines of public int method1(int a, int b, int c){ //method body } public int method1(int a, int b){ method1(a,b,nul l} public int method1(int a){ method1(a,null, null) }
I understand that int is a value type and so can't be null, but all the examples i find using value types for overloading pass 0, which is no good for me as this is a meaningful value for the parameter, i want to pass something that says the parameter is empty. I have tried using Int32 and this complains that it can't be null too.
I have considered creating my own object wrapping int just so i can pass null, but that seems absurd. Is there an object (equivalent to Java's Integer) that i have missed, or are you supposed to do overloading in some different way in this situation?
many thanks
James
Hi James,
The way method or function overloading works is that you have to define all
the functions with different signatures, then the compiler will figure out
which one to use automatically.
In your case you should do this:
public int method1(int a, int b, int c){
//method body
}
public int method1(int a, int b){
//method body
}
public int method1(int a){
//method body
}
Then, from your code you can use these methods like:
methond1(10, 12, 13);
or
method1(10, 12)
or
method1(10);
The complier will figure out which particular function to call based on the
signature.
"james b" <j@mesbridger.c om> wrote in message
news:be******** *************** ***@posting.goo gle.com... Hi all, I'm trying to do something with method overloading and I can't seem to get it to work
my code is along the lines of public int method1(int a, int b, int c){ //method body } public int method1(int a, int b){ method1(a,b,nul l} public int method1(int a){ method1(a,null, null) }
I understand that int is a value type and so can't be null, but all the examples i find using value types for overloading pass 0, which is no good for me as this is a meaningful value for the parameter, i want to pass something that says the parameter is empty. I have tried using Int32 and this complains that it can't be null too.
I have considered creating my own object wrapping int just so i can pass null, but that seems absurd. Is there an object (equivalent to Java's Integer) that i have missed, or are you supposed to do overloading in some different way in this situation?
many thanks
James
Hi Bob,
Thanks for your suggestions
I've always had a problem with the "magic number" solution, always
seemed like such a hack :)
Using Objects and then unboxing is something i had considered - but i
felt i would rather wrap the int in another class to retain some
notion of the correct type - I think I will probably end up going this
way.
I'm pretty sure i tried using SqlInt32 and Int32 and it still
complained about the nulls, although i'm at home now so i can't verify
the SqlInt32.
James
"Bob Grommes" <bo*@bobgrommes .com> wrote in message news:<eX******* *******@TK2MSFT NGP10.phx.gbl>. .. Value types simply can't be null. Your choices would be:
1) Use a "magic number" to signifiy empty, such as Int32.MinValue.
2) Make the 2nd & 3rd arguments object, and unbox the int if not null
3) Use SqlInt32 instead of int. Then you can test for, say, b.Value == b.Null. This is similar to what you were thinking of doing -- wrapping the integer in a class. It's already been done for you.
--Bob
"james b" <j@mesbridger.c om> wrote in message news:be******** *************** ***@posting.goo gle.com... Hi all, I'm trying to do something with method overloading and I can't seem to get it to work
my code is along the lines of public int method1(int a, int b, int c){ //method body } public int method1(int a, int b){ method1(a,b,nul l} public int method1(int a){ method1(a,null, null) }
I understand that int is a value type and so can't be null, but all the examples i find using value types for overloading pass 0, which is no good for me as this is a meaningful value for the parameter, i want to pass something that says the parameter is empty. I have tried using Int32 and this complains that it can't be null too.
I have considered creating my own object wrapping int just so i can pass null, but that seems absurd. Is there an object (equivalent to Java's Integer) that i have missed, or are you supposed to do overloading in some different way in this situation?
many thanks
James
Now that is creative !!!
I'll end up with some very messy signatures inside the class, as I'm
actually using several more parameters than i put in my examples, but
my external interface to the class will look the way i want it.
I think this is the approach i will take, i just wish i didn't have to
jump through these hoops to achieve something that should be so
simple...
many thanks
James
"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message news:<Od******* *******@tk2msft ngp13.phx.gbl>. .. James, I would consider a variation of Bob Grommes's second item: 2) Make the 2nd & 3rd arguments object, and unbox the int if not null
Have a fourth helper function that the three overloaded functions call. This helper function has extra parameters to identify whether b & c are null or not.
public int method1(int a, int b, int c) { method1(a, b, false, c, false); } public int method1(int a, int b) { method1(a,b,fal se, 0, true); } public int method1(int a) { method1(a, 0, true, 0, true); }
private int method1(int a, int b, bool bNull, int c, bool cNull) { //method body }
The helper function is private as it is not intended to be called outside the function. This avoids boxing & unboxing of the parameters...
Hope this helps Jay
"james b" <j@mesbridger.c om> wrote in message news:be******** *************** ***@posting.goo gle.com... Hi all, I'm trying to do something with method overloading and I can't seem to get it to work
my code is along the lines of public int method1(int a, int b, int c){ //method body } public int method1(int a, int b){ method1(a,b,nul l} public int method1(int a){ method1(a,null, null) }
I understand that int is a value type and so can't be null, but all the examples i find using value types for overloading pass 0, which is no good for me as this is a meaningful value for the parameter, i want to pass something that says the parameter is empty. I have tried using Int32 and this complains that it can't be null too.
I have considered creating my own object wrapping int just so i can pass null, but that seems absurd. Is there an object (equivalent to Java's Integer) that i have missed, or are you supposed to do overloading in some different way in this situation?
many thanks
James
james b <j@mesbridger.c om> wrote: Now that is creative !!! I'll end up with some very messy signatures inside the class, as I'm actually using several more parameters than i put in my examples, but my external interface to the class will look the way i want it. I think this is the approach i will take, i just wish i didn't have to jump through these hoops to achieve something that should be so simple...
The thing is, it's *not* simple. If you really need every value
available in int to be a valid value, but you also need "this parameter
is valid" information, then you basically need more information than is
available in an int, so you've got to have *something* extra - whether
that's a reference or an extra boolean, you've got to have that
information somewhere.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Terje Slettebø |
last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been
discussed before, I'd appreciate a pointer to it. I again haven't found it
in a search of the PHP groups.
The PHP manual mentions "overloading"
(http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't
really overloading at all... Not in the sense it's used in other languages
supporting overloading (such as C++ and Java). As one of the...
|
by: Zenon |
last post by:
Folks,
I am having a terrible time overloading operators. I have tried what
I thought was the correct way, I tried the cheating (friend
declarations), all to no avail. Sorry for posting tons of code but I
really need help.
thanks,
Zenon
|
by: ryan.fairchild |
last post by:
I have a problem I am trying to create a MyInt class to hanlde very
large ints. Its for a class, therefore I can only do what the teach
tells me. I want to be able to overload the insertion operator so that
I can read in one digit at a time from the buffer and if the digit is 0
- 9 then put it into the dynamic array which stores each digit of this
large int.
#include <iostream>
#include <string>
#include "myint.h"
|
by: KL |
last post by:
I am working on a school assignment, so please don't tell me the
solution. I just want some direction.
I am supposed to overload the >, <, ==, !=, >=, and <= operators using
bool. I am having a bit of a problem in seeing what needs to happen
here. I am just not sure how I do this. Will the overloading function
recognize a < and a usual <? Do I do an
IF (a.letters < b.letters){
return true
}
|
by: jacob navia |
last post by:
C++ introduced an interesting feature (among others): operator overloading.
The idea is to build a mechanism for the user defining its own number types and the operations to be
done with them.
I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350
bit floats.
Yes, 350. We have a lot of fast machines now. Why not use them? The extra-precision is useful in
some algorithms.
| |
by: md |
last post by:
Hi,
the following code is working for static objects.
ie the statement IntArray x(20);
my problem is i want to use this overloading operator for
dynamically created objects
for example the statement IntArray *x;
Please give me some help regarding this
//////////////////////////////////////////////////////////////////////////////////////////////////////////
class IntArray
|
by: sarathy |
last post by:
Hi all,
I just wanted to know if it is possible to overload the
original meaning of operators. I read that it is not possible to
change the operator precedence by using operator overloading. Other
than that i think that the operator functionality can be overloaded.
But in the following code, the operator + has been overloaded on type
int. But it does'nt seem to work as the operator fn was not invoked.
In the following code,
|
by: Hayrola |
last post by:
I have two vaiable types, named A and B. These are of the same type,
int, but their values must be treated differently before printing. eg:
typedef int A;
typedef int B;
void func(A val)
{
VIPfunction(val);
std::cout << val << std::endl;
|
by: Mr.SpOOn |
last post by:
Hi,
in a project I'm overloading a lot of comparison and arithmetic
operators to make them working with more complex classes that I
defined.
Sometimes I need a different behavior of the operator depending on the
argument. For example, if I compare a object with an int, I get a
result, but if I compare the same object with a string, or another
object, I get another result.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |