object in bool context

This is a discussion on object in bool context within the C++ Programming forums, part of the General Programming Boards category; Well ... first please see this example using C++ ifstream: Code: #include <iostream> #include <fstream> using namespace std; int main(int ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    Talking object in bool context

    Well ... first please see this example using C++ ifstream:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char** argv)
    {
      if (argc != 2)
      {
        cout << "Please give a file name." << endl;
        return 1;
      }
    
      ifstream in(argv[1]);
      if (in)
      {
        cout << "Success!" << endl;
        in.close();
      }
      if (!in)
      {
        cout << "Failure!" << endl;
        return 2;
      }
     
      return 0;
    }
    My question is how does ifstream object behave like a bool value in above code?
    I want to write my own class. And I wish it's objects would behave in the same way.
    Can you please tell me how can I write such a class?
    Last edited by manav; 01-18-2008 at 04:02 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    There are several ways of doing this, but some ways are better than others. This article explains the differences between the different methods, and why some are better than others.

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Oh My GOD!
    I didn't even know about
    Code:
    operator bool() { ... }
    :shock:
    And I experimented with
    Code:
    operator int() { ... }
    This also worked fine.
    Wow! Thanks!

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Wow!
    All of this works fine! And I never knew it!
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Cool
    {
      bool bval;
      int ival;
      const char* pval;
    public:
      operator bool() { return bval; }
      operator int() { return ival; }
      operator const char*() { return pval; }
    
      Cool& operator =(bool b) { bval = b; return *this; }
      Cool& operator =(int i) { ival = i; return *this; }
      Cool& operator =(const char* p) { pval = p; return *this; }
    };
    
    int main(int argc, char** argv)
    {
      Cool val;
      val = false;
      cout << boolalpha << static_cast<bool>(val) << endl;
      val = 55555;
      cout << static_cast<int>(val) << endl;
      val = "Amazing!";
      cout << static_cast<const char*>(val) << endl;
      return 0;
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, no, no! No abuse of implicit conversion. It leads to horrible code.
    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

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Ok CornedBee.
    I was just excited about this new (for me) feature of C++
    I will not abuse this feature. I was just testing ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesn't this example work for me?
    By xixpsychoxix in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 09:25 PM
  2. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  3. BOOL bool ? unresolved external symbol
    By xwielder in forum C Programming
    Replies: 6
    Last Post: 05-20-2008, 09:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Object returning a bool
    By PJYelton in forum C++ Programming
    Replies: 15
    Last Post: 11-27-2002, 11:15 AM

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