Topic: A good bool example (I think)


Author: rrowe@halcyon.com (Robin Rowe)
Date: 24 Jan 1994 13:59:16 -0800
Raw View
Reply To: Alan Waldock ajw@ornews.intel.com (Sat Jan 22 16:21:44
PST 1994)
Subject: Re: new bool type

Alan,

Hope you find this under the new thread. It was getting hard to
follow all in one thread.

<< Look at your own classes that already have a cast-to-int or a
cast-to-pointer; aren't there cases where being able to use an
object name as a boolean expression (perhaps for state, like
iostreams) would be desireable? >>

That's a good point, and suggests the strongest example presented
so far. It would be nice to not have to write both an operator!
and an operator int/void*. Moreover, I have a PathFileName class
that has an operator const char*. That class would definitely
benefit from an operator bool since overloading operator int or
operator void* is ambiguous in conjunction with operator const
char*. My PathFileName class is actually dangerous as it stands
now since it has an operator!, but its operator const char* will
never return 0.

class PathFileName
{   char* path;
    unsigned int offset;
public:
    PathFileName(const char* const path,const char* const
fileName);
    ~PathFileName()
    {   delete[] path;
        path=0;
    }
    int operator!() const
    {   return path==0;
    }
    operator const char*() const
    {   return path ? path : BLANK;//dangerous: if(object) always
true.
    }
//  ambiguous with operator const char*:
//  operator int() const
//  {   return !(!(*this));
//  }
    const char Drive() const
    {   return (path&&(path[1]==':')) ? *path : '?';
    }
    const char* FileName() const
    {   return path ? path+offset : BLANK;
}   };

Another point with this example is that if bool is a class then
for an object of type PathFileName you can not do
cout<<bool(object).

My question to you and everyone else here is: does this example
withstand scrutiny as a good reason to have a built-in bool type?

Thanks!

--
-------------------------------------------------------
Robin Rowe    Rowe Techology | Flamers and complainers:
rrowe@halcyon.com            |  If you have nothing to
Northwest C++ Users Group    |   contribute please don't.