By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

overloading vs. default argument question

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.