Simple question: How to assign defalut value to bool& ?

This is a discussion on Simple question: How to assign defalut value to bool& ? within the C++ Programming forums, part of the General Programming Boards category; The following code is correct and simple Code: void foo(bool& valid) { valid = true; return; } void foo2(string& str) ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Simple question: How to assign defalut value to bool& ?

    The following code is correct and simple
    Code:
    void foo(bool& valid)
    {
      valid = true;
      return;
    }
    
    void foo2(string& str)
    {
      str = "abc";
      return;
    }
    
    void main()
    {
    bool valid;
    foo(valid);
    string str;
    foo2(str);
    return;
    }
    Now what if I want give bool& and string& a default value so that someone can call foo() and foo2() without parameters

    Code:
    void foo(bool& valid = bool(true) )
    {
      valid = true;
      return;
    }
    
    void foo2(string& str=string(""))
    {
      str = "abc";
      return;
    }
    
    void main()
    {
    bool valid;
    foo();
    foo2();
    return;
    }
    Why it doesn't work?

  2. #2
    coder
    Join Date
    Feb 2008
    Posts
    127
    first:
    Code:
    void main ()
    should be
    Code:
    int main ()
    one simple solution to your problem may be:
    -declare the variables as global (out of main)
    -set their value inside main

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Global variables are best avoided.
    Now, why would the arguments need to have default parameters anyway since you are going to modify them?
    Const arguments may have default values, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm not entirely certain you can use default parameters with references. (Normally it would just be
    Code:
    void foo(bool valid = true)
    On the other hand, the "right" answer is going to depend on context. If you are expecting to change the value of the parameter inside the function, what does it mean to not give the function a parameter to change?)

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks all. I am doing this because I am working on someone else's old code. I need to revise the method foo/foo2 a little to modify a bool/string, but not all callers of this method need that -- and they are in multiple places of the old code that I don't want to change them.

    So the solution is to give a default value. If I need to modify a bool/string, I can pass one in. For the previous callers I don't need to revise them
    Last edited by meili100; 02-26-2008 at 02:50 PM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Quote Originally Posted by tabstop View Post
    I'm not entirely certain you can use default parameters with references. (Normally it would just be
    Code:
    void foo(bool valid = true)
    On the other hand, the "right" answer is going to depend on context. If you are expecting to change the value of the parameter inside the function, what does it mean to not give the function a parameter to change?)
    It is possible with references.
    Code:
    void foo(const string& r = string(""));
    So long as it's const it works fine.

    Quote Originally Posted by meili100 View Post
    Thanks all. I am doing this because I am working on someone else's old code. I need to revise the method foo/foo2 a little to modify a bool/string, but not all callers of this method need that -- and they are in multiple places of the old code that I don't want to change them.

    So the hack solution is to give a default value. If I need to modify a bool/string, I can pass one in. For the previous callers I don't need to revise them
    The solution I see is to overload the function.
    Code:
    void foo(/*...*/, bool& b)
    {
    	// Code
    	b = true;
    }
    
    void foo(/*...*/)
    {
    	bool b;
    	foo(b);
    }
    That, or use pointers.
    Last edited by Elysia; 02-26-2008 at 02:57 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Yeah, non const references can't have a default values. The normal solution is to use a pointer instead. But that's not a quick fix, because you still have to add code to handle the null pointer case.

    I suppose you can have a global default, but that's very hackish, and best avoided. Better take a little to fix it the right way now, rather lather on levels of complexity.
    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.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Elysia's solution ought to work very well. No need for hacks.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    But a const bool& parameter is kind of pointless isn't it?
    If the bool doesn't need to be modified, pass it by value instead of reference.
    If it does need to be modified and you want a default parameter there are 2 options I can see:
    Code:
    void func( bool*  pB = NULL )
    {
        ...
        if ( pB != NULL )
        {
            ...
            pB = true;
        }
    }
    or
    Code:
    void func( bool&  b ) // For when you care about what b gets set to...
    { ... }
    
    void func() // For when you don't care what b gets set to...
    {
        bool b = true;
        func( b );
    }
    I would prefer the 2nd option since you don't have to deal with NULL pointers...

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    The problem here is the op doesn't want to go to every call location and make the necessary changes. So none of those solutions work.

    Conversely, there's no solution to the op problem without having to go to every call location and make changes
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Overloading should work very fine. Just modify the original function to add the extra functionality and overload another function that does not have this argument as a stub that simply wraps the call to the original function.
    No (existing) function calls will need to be changed. I don't see how they would have to be?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    My bad. Misread the op original post. Time to bed.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question
    By spikerotc04 in forum C Programming
    Replies: 4
    Last Post: 04-26-2005, 07:23 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. A Simple Question
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 10-26-2001, 05:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21