Thread: I wish to give a const & parameter a default value

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question I wish to give a const & parameter a default value

    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.
    Last edited by Programmer_P; 10-23-2011 at 12:55 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You should use a templates:
    Code:
    template <bool requiredValue>
    void setRequired(int elementWhichRequiresIt);
    
    template <>
    void setRequired<true>(int elementWhichRequiresIt){
      //required is true.
    }
    
    template <bool requiredValue>
    void setRequired();
    
    template <>
    void setRequired<false>(){
      //required is false
    }
    
    int main(){
      setRequired<true>(1);
      setRequired<false>();
    
      //setRequired<true>(); <-error
      //setRequired<false>(1); <-error
    }
    After all, bool required value is a compile time parameter, logically speaking. It's validity should be caught compile time, not when the program is run.
    Last edited by King Mir; 10-23-2011 at 01:37 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Actually, why not just have setRequired() and setRequired(C_html_elementelementWhichRequiresIt)? The template solution I posted earlier is useful only if exactly one of the two listed errors is unwanted.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redefinition of default parameter
    By 843 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2011, 02:07 AM
  2. Replies: 9
    Last Post: 06-18-2009, 04:58 AM
  3. Default parameter
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2007, 11:59 AM
  4. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 04:21 PM
  5. default parameter
    By laasunde in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2003, 10:55 PM