Hello,
I have the following function:
Code:
void setRequired(bool requiredValue, const C_html_element& elementWhichRequiresIt) {
if (requiredValue == true) {
required.first = requiredValue;
required.second = elementwhichRequiresIt;
}
else {
required.first = requiredValue;
}
}
Basically, I want the function to be able to handle calls like this:
Code:
C_html_element obj;
obj.setRequired(false);
when the required value needs to be false, and it needs to handle calls like this:
Code:
C_html_element obj;
C_html_element obj2;
obj.setRequired(true, obj2);
when the required value needs to be true. Now, what I was wanting is something similiar to:
Code:
void setRequired(bool requiredValue, const C_html_element* elementWhichRequiresIt = NULL) {
only with a const reference instead of a pointer. And obviously, I realize that I could easily create an empty C_html_element object as a private member of the "C_html_element" class, and then just initialize the "elementWhichRequiresIt" parameter to that private member, but I was hoping to avoid that approach, since that is just an object doing nothing but taking up space in memory, and wont ever be used except for the purpose above. I have also considered using an overloaded version of setRequired, which does not include the second parameter of the above version, but the problem with that is, then it would allow calls like this:
Code:
C_html_element obj;
obj.setRequired(true);
which I don't want. So the next step of my thought processes was to do this instead:
Code:
void setRequiredToTrue(const C_html_element& elementWhichRequiresIt) {
required.first = true;
required.second = elementWhichRequiresIt;
}
void setRequiredToFalse() {
required.first = false;
}
But then that's two different function names to remember, and I kinda want to use just one for the purpose of setting the required value of a C_html_element: setRequired().
Any ideas?
Thanks in advance.
EDIT: Nevermind. I'll just overload it, and then throw an exception if the caller attempts to pass a value of true to the overloaded version.