Thread: boolean evaluating objects

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    boolean evaluating objects

    Hi,
    I was wondering how is something like fail-bit evaluation of cin implemented. I am trying to write a class:
    Code:
    class A {
         public:
              bool b();
    };
    such that -
    Code:
    int main() {
         A a_inst;
         if (a_inst) {
              //blah blah
         }
    }
    is equivalent to -
    Code:
    int main() {
         A a_inst;
         if (a_inst.b()) {
              //blah blah
         }
    }
    is it possible?

    Thank you very much

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm supposing that your code checks if the object is "true?"
    If so, you could overload operator bool and call member function b.
    If not, then I discourage you from doing it because it creates hard to read code, not to mention it's a bad habit.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Thank you for your reply.

    I'm supposing that your code checks if the object is "true?"
    Yes, that is what I am trying to do.

    If so, you could overload operator bool and call member function b.
    That sounds like what I am looking for, can you please give an example of how that is done?

    Thank you

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    cin is a little more complicated than a simple operator bool. Stream classes overload operator void* which can be evaluated as a bool.

    Either way, make sure that it makes sense for your class. It can lead to weird consequences and often just the regular function will work well enough.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make sure it makes sense before doing it, first.
    Code:
    class my_class
    {
    	operator bool () const;
    };
    
    my_class::operator bool() const
    {
    	return (expression_here);
    }

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Thank you for your replies.

    I have not actually came up with a case where I intend to use this, but was just wondering about how is it done.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I believe streams use operator void* to stop the user from accidentally passing a stream to a function that takes a bool. If you use operator bool, then this would work:
    Code:
    void myclass::output(bool showHeader)
    {
      if (showHeader)
        std::cout << header;
      std::cout << data;
    }
    
    int main()
    {
      outputclass oc;
    
      std::ofstream fout("file.txt");
      oc.output(fout); // Doesn't compile
    
      my_class mc;
      oc.output(mc); // compiles, but maybe doesn't do what you want
    }
    That first try won't compile because the stream overloads operator void* and you can't do two implicit conversions. The second try will compile, which only makes sense if your my_class works like a bool.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I see what you mean now.

    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM