Thread: Convert class object to bool

  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573

    Convert class object to bool

    Hi,
    I was wondering how I could achieve the syntax like file streams for my classes.
    Code:
    std::fstream fin(sth);
    if (fin)//?
    The constructor would set an error flag. This would return true if error_flag == 0.
    Should I overload an operator and which (I tried !)?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The fstream has an automatic conversion to void*, and that void* evaluates to true for non-zero and false for zero. So you would overload operator void*() and have it return 0 for a failed state.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, thanks.

    I googled for "bool operator()" and it turned out "operator bool()" was what I was looking for Never heard of that before.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That will work, too. There is a reason that fstreams use the operator void* instead of bool. I don't remember it exactly, though. My guess is that assigning an fstream to a bool would happen automatically, which you don't really want. Having an automatic conversion to void* is much less likely to cause errors in your program. For example, if you accidentaly send an instance of your class to a function expecting a bool, the compiler won't complain, but your code might not work.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I believe the reason is that operator bool() would allow implicit casting to int, which may not be desirable.
    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

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ok thanks. I got this and it is working nicely.

    Code:
    Maze::operator void*()
    {
        return errorState ? 0 : this;
    }
    Learnt something new today, although it's just "syntactic sugar"

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The Boost libraries have a very special technique for converting to boolean, which avoids the problems of void* too. I can't remember it, though.
    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

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  3. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 09:20 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM