473,399 Members | 4,192 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,399 software developers and data experts.

overloading vs. default argument question

So lets say I have this pretend function that compares a given serial
number against the one true serial VALID_SERIAL:

bool validateSerial(string serialNum)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}
I want to add a function that does the same thing, but will also pad
the given serialNum with a prefix of "0" if the original serialNum
does not check out. I was thinking I could change the original
function to something like:

bool validateSerial(string serialNum, bool padFlag = false)
{
if(!padFlag)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}
else
{
// padding functionality...unimportant for now
}
}
but that would seems ugly to have one function that should be really
be two. I could also overload it with a new function like

bool validateSerial(string serialNum, bool padFlag)
{
//padding functionality
}

but that function would not even use the padFlag variable.


Any suggestions on how I should organize these public functions from
an OO perspective?

Thanks

Mar 20 '07 #1
2 1414

pe*******@gmail.com je napisao:
So lets say I have this pretend function that compares a given serial
number against the one true serial VALID_SERIAL:

bool validateSerial(string serialNum)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}
I want to add a function that does the same thing, but will also pad
the given serialNum with a prefix of "0" if the original serialNum
does not check out. I was thinking I could change the original
function to something like:

bool validateSerial(string serialNum, bool padFlag = false)
{
if(!padFlag)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}
else
{
// padding functionality...unimportant for now
}
}
but that would seems ugly to have one function that should be really
be two. I could also overload it with a new function like

bool validateSerial(string serialNum, bool padFlag)
{
//padding functionality
}

but that function would not even use the padFlag variable.


Any suggestions on how I should organize these public functions from
an OO perspective?

Thanks
>From OO perspective both forms are same. I will probalby use
bool validateSerial(string serialNum, bool padFlag = false)

and implement both functionality in private methodes. But, this is
just main opinion, not some guide.

Best,
Zaharije Pasalic

Mar 20 '07 #2
On 20 Mar, 12:34, perkin...@gmail.com wrote:
So lets say I have this pretend function that compares a given serial
number against the one true serial VALID_SERIAL:

bool validateSerial(string serialNum)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}

I want to add a function that does the same thing, but will also pad
the given serialNum with a prefix of "0" if the original serialNum
does not check out. I was thinking I could change the original
function to something like:

bool validateSerial(string serialNum, bool padFlag = false)
{
if(!padFlag)
{
if(serialNum == VALID_SERIAL)
return true;
else
return false;
}
else
{
// padding functionality...unimportant for now
}
}
That function doesn't do what you say. Your words say you want to pad
if the serial number doesn't check out, but your code says you want to
pad if the padFlag is set. Which is it? If the words are right and the
code is wrong, should the code really be this?

bool validateSerial(string serialNum, bool padFlag = false)
{
if(serialNum == VALID_SERIAL)
return true;
else
{
if (padFlag)
{
// padding functionality...unimportant for now
}
return false;
}
}

That seems to me to be what your words described, and seems tidy
enough. I'd consider putting the padding functionality in a function
of its own if it's anything more than a few lines.

On the other hand, if it's your code that's right and your words that
are wrong, then this would seem to do what you need:

bool validateSerial(const string& serialNum)
{
return serialNum == VALID_SERIAL
}

bool someFunctionName(string serialNum, bool padFlag = false)
{
if(!padFlag)
{
return validateSerial(string serialNum)
}
else
{
// padding functionality...unimportant for now
// but it will need to return a bool somehow
}
}

Again, the padding functionality could go in its own function. This
seems to do the same as your code above, but I am struggling to think
of a name for someFunctionName because padding and validating seem
like totally different things that should not be in the same function
at all.

One final point - I feel I may have missed something as throughout all
this you are passing your string by value so any changes you make
during padding will only affect the local copy, not the string passed
by the caller. Should the string be being passed by reference?

Gavin Deane
but that would seems ugly to have one function that should be really
be two. I could also overload it with a new function like

bool validateSerial(string serialNum, bool padFlag)
{
//padding functionality
}

but that function would not even use the padFlag variable.

Any suggestions on how I should organize these public functions from
an OO perspective?
Mar 20 '07 #3

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

Similar topics

3
by: Frank Bechmann | last post by:
Eventually most of you will not learn much from this because it's just another event in the 'default argument value gotcha' series, but because it cost me some hours yesterday to spot this 'error'...
5
by: Paul Sweeney | last post by:
The python tutorial gives the following example to demonstrate the fact that default args are only evaluated once: def f(a,L=): L.append(a) return L print f(1),f(2),f(3)
3
by: CoolPint | last post by:
Can anyone explain how I can make the following function accept an default arguement for the last parameter, which should be an optional functor? template <typename T, typename FUNCTOR> void...
2
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
4
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default...
2
by: =?gb2312?B?wfXquw==?= | last post by:
Hi folks, I am trying to add a default argument to a template function in a template class, here is the code snippet: template<typename T> class Test { template<typename U> friend void...
1
by: Paulo J. Matos | last post by:
Hi all, Going through the tutorial brought up a question. Consider the functions: def f(a, L=): L.append(a) return L print f(3) print f(9)
1
by: Chris Rebert | last post by:
On Wed, Oct 29, 2008 at 9:14 AM, Paulo J. Matos <pocm@ecs.soton.ac.ukwrote: http://effbot.org/zone/default-values.htm explains this FAQ quite well. Note that the "accumulation" behavior of lists...
3
by: evenstar | last post by:
As I know"A nonstatic data member may not be used as a default argument because its value cannot be used independently of the object of which it is a part. Using a nonstatic data member as a default...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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.