Previous section   Next section

Imperfect C++ Practical Solutions for Real-Life Programming
By Matthew Wilson
Table of Contents
Chapter 24.  operator bool()


24.2. operator void *() const

But we know that int is not the only type that can be used in expression evaluation (see section 15.3). In pre-C++-98 times, the int problems were somewhat ameliorated by providing operator void*() const. The original IOStreams ios class did so, and its modern template form basic_ios carries the same method.

Listing 24.2.


class ExpressibleThing


{


public:


  operator void *() const;


};





ExpressibleThing  e;





if(e)   // Evaluates whether e "is"


{}


if(!e)  // Evaluates whether e "is not"


{}



Alas, the implicit conversion problem hasn't gone away, it's just been moved sideways. Now you can inadvertently convert to any pointer type through a simple cast. Even worse, the following is legal, though more than a little incorrect:



ExpressibleThing  e;








delete e; // Nasty!



Needless to say, this is not good enough.


      Previous section   Next section