Convert class object to bool

This is a discussion on Convert class object to bool within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering how I could achieve the syntax like file streams for my classes. Code: std::fstream fin(sth); if ...

  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,340
    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,340
    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
    21,893
    I believe the reason is that operator bool() would allow implicit casting to int, which may not be desirable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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
    Portugal
    Posts
    7,510
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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, 04: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, 08:20 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

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