Thread: Object returning a bool

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    Object returning a bool

    I'm programming a connect four game and I ran into an interesting question which I haven't seen addressed anywhere. Say I have an object called board which stores a board position among other things. Now, say I want to check to see if somebody has four in a row, obviously I could use a member function like board.checkIfWon(), but is there any way to do something like this:
    Code:
    if (board)
       // somebody won
    
    *OR*
    
    if (!board)
       // keep playing
    Any way to overload anything to accomplish this? Not saying this is the best or easiest way to go, I was just curious if it was possible.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    my vote is it can't be done like that, but my experience is limited. Others will undoubtedly weigh in as well.

    As I understand it, the conditional of the if must resolve to a true or false value. Pointers can be false as NULL is interpretted as zero which is false, but I don't think objects as such can be interpretted as true or false.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    I don't think so. I think the only time something similar to that would is if you're just creating the object and its constructor returns 1 or 0.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I've asked this question before, and this is the answer I got:
    Code:
    class MYCLASS
    {
       public:
          MYCLASS();
    
       private:
          bool Status;
    };
    
    MYCLASS:MYCLASS() : Status(0)
    {
       ...
    }
    I'm not quite sure what actually happens. If you print only the object name, the value of Status is returned, but sometimes it doesn't (especially when using nested overloaded operators).

    You could also overload the () operator to return the value. You have to print MyObject() instead of Myobject, but it might be better than using a real method.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Any way to overload anything to accomplish this?
    Yes, overload operator bool. But think long and hard about something like this, such conversions tend to be more trouble then they're worth.
    Code:
    #include <iostream>
    
    class test
    {
      bool won;
    public:
      test ( bool init )
        : won ( init )
      {}
    
      operator bool()
      {
        return won;
      }
    };
    
    int main()
    {
      test t ( true );
    
      if ( t )
        std::cout<<"You won!\n";
      else
        std::cout<<"You lost\n";
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    nameless...
    Guest
    you might be better of overloading the ! to get the second effect, it would be much easier!

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >you might be better of overloading the ! to get the second effect, it would be much easier!
    Of course then you don't get the inverse operation. You can do

    if ( !board ) // Okay

    but not

    if ( board ) // Bzzt! Error!

    which would cause nothing but confusion. I would favor a separate member function won() which returns a bool. You maintain similar syntax, and save yourself the trouble of implicit type conversion:

    if ( !board.won() ) // Okay

    and

    if ( board.won() ) // Also okay

    -Prelude
    My best code is written with the delete key.

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Okay, cool. I was pretty sure that any solution would be 10 times more of a pain in the rear than just making a member function, and looks like thats true

    I think its fascinating to learn different ways to do things though, like I had no idea one could overload bool! Thanks!

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I like the syntax
    Code:
    if (board.checkIfWon())
    more than
    Code:
    if (board)
    The latter requires a comment to clarify what actually is being tested.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I thought bool was a type, not an operator????? How can you overload a type?


    bool won;//bool used as a type
    public:
    test ( bool init )//bool used as a type
    : won ( init )
    {}

    operator bool()//bool used as an operator???


    I'll have to go back and review syntax of conversion operators.........again. Sighhhhhhhh

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Prelude
    if ( !board ) // Okay

    but not

    if ( board ) // Bzzt! Error!
    How about:

    if(!!board)

    That would probably work, though a little inconvenient .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    how can you overload a type
    You don't, you just overload the operator that converts a Board to a bool, since the compiler doesn't know how to do that automagically.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Magos
    I've asked this question before, and this is the answer I got:
    Code:
    class MYCLASS
    {
       public:
          MYCLASS();
    
       private:
          bool Status;
    };
    
    MYCLASS:MYCLASS() : Status(0)
    {
       ...
    }
    I'm not quite sure what actually happens. If you print only the object name, the value of Status is returned, but sometimes it doesn't (especially when using nested overloaded operators).

    You could also overload the () operator to return the value. You have to print MyObject() instead of Myobject, but it might be better than using a real method.
    I don't think so, because this initializes, the private member status to zero, it's called the member initializer.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by elad
    I thought bool was a type, not an operator????? How can you overload a type?


    bool won;//bool used as a type
    public:
    test ( bool init )//bool used as a type
    : won ( init )
    {}

    operator bool()//bool used as an operator???


    I'll have to go back and review syntax of conversion operators.........again. Sighhhhhhhh

    You are basically telling the compiler "If you see this object out of place call this if it fits.".

    ie:


    Code:
    class person { public:
    person(int years)
     :age(years)
     { /*  */ }
    operator int ( ) { 
     return age;
     }
    private:
    int age;
    };
    
     person Mike(27);
     person Peter(55);
    
     if(Mike > Peter)
      cout << "Mike is older, at the age of " << Mike;
     if(Mike < Peter)
      cout << "Peter is older, at the age of " << Peter;
     else
      cout << "Peter and Mike are the same age";
    
     getch();
    
    return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The latter requires a comment to clarify what actually is being tested.
    The latter can also be used in oopsie situations when the programmer really didn't mean to do it, but because of the implicit conversion she/he wasn't warned. I really dislike using such conversion overloading, explicit member functions are easier, clearer, and safer in pretty much every situation.
    How about :

    if(!!board)

    That would probably work, though a little inconvenient .
    My primary argument against overloading ! was that not having the common inverse operation would confuse people. Negating twice really isn't any better.

    >I thought bool was a type, not an operator????? How can you overload a type?
    You don't overload the type, you overload the implicit conversion to that type. Assuming you overload bool, if the compiler sees an operation performed on the object that doesn't work naturally but would work when converted to bool, the conversion is made.

    I know how you feel though, the first time I saw implicit conversion overloading I was just a little confuzzled.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. How do I play an MP3?
    By Hunter2 in forum Windows Programming
    Replies: 28
    Last Post: 05-20-2002, 08:49 PM