Thread: Passing a boolean var by reference

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    Passing a boolean var by reference

    Hi,
    I have a boolean varible set to true.When I attempt to set it to false from within a function the change isnt passed back to the varible.

    If I change the varible to an int,and the function to return an int it works.The compiler doesnt raise any errors,so the syntax is ok..

    Code:
    bool reDeal=true;
    
    bool anotherGo(bool &reDeal)
    {
      reDeal=false;
      return reDeal;
    }
    Is it just that you cant pass boolean varibles by reference?

    Regards

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So what does the call look like?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And I'm hoping you aren't expecting to change the global variable of that name, unless you're passing it into the function.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    I take it you mean when i call the function.Excuse my ignorance!

    Code:
    anotherGo(reDeal);
    orignaly i had more code within the function declaration but got rid of it all and just set it up so it was as simple as possible just in case I was doin something wrong

    thanks

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    #include <iostream>
    #include <iomanip>
    
    bool anotherGo(bool &reDeal)
    {
      reDeal = false;
      return reDeal;
    }
    
    int main() {
    
        bool reDeal = true;
        std::cout << std::boolalpha;
        std::cout << reDeal << std::endl;
        anotherGo(reDeal);
        std::cout << reDeal << std::endl;
        return 0;
    }
    Code:
    $ ./redeal
    true
    false

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    thanks tap,that compiled fine and did change the value where was I going wrong?
    I had set up an if loop after the function to cout 'true'if was it true after the function was used and 'false'if it wasnt,and it always returned the var unchanged

    what does std::boolalpha do?

    the only libarys i was using was <iostream> and <ctime>for the random numbers

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'd be willing to bet 37 cents that you did
    Code:
    if (reDeal = true)
    instead of
    Code:
    if (reDeal == true)
    or the even more correct
    Code:
    if (reDeal)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cathalo
    what does std::boolalpha do?

    the only libarys i was using was <iostream> and <ctime>for the random numbers
    You might want to read cppreference.com's entries on C++ I/O.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    Thumbs up

    @tabstop-yep thats what i did..that has caught me out before.When i use a single'=' I'm giving the varible a value as opposed to testing the if loop that reDeal 'equals' what ever value.Something simple and i was looking for something complex,

    will look at the C++ refernce laserlight

    Thanks again

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    And I owe you 37 cents Tabstop

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you turn up your compilers warning level then they tend to catch things like that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    will look into that imalc using visual express

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Project -> properties -> C/C++ -> General -> Warning Level -> Level 4 (W4)
    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.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    got that elysia thanks,would of been all day tryin to find that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help assignment
    By 6kaine9 in forum C Programming
    Replies: 26
    Last Post: 10-19-2008, 08:51 PM
  2. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM