TITLE: testing the validity of an object instance (Newsgroups: comp.lang.c++.moderated, 23 Jul 97) [ What is the best way to code a method that returns the validity of an arbitrary object instance? ] SUTTER: herbs@cntc.com (Herb Sutter) Short answer: Use operator! or a named member function. Throwing in an implicit conversion just to get this kind of syntactic sugar is like detonating a tactical nuclear weapon to get rid of an annoying fly. Longer answer follows: HOWARD: walth@netcom.com (Walt Howard) > Yes. Three ways I can think of, operator!(), operator int(), >operator bool(). SUTTER: I really would advise against the last two. There are many potential problems with supplying conversion operators. If you must supply a conversion operator to get this syntactic sugar, the best choice is probably operator void*() since it's moderately less likely to cause problems, but I wouldn't advise that either. To avoid hidden conversions and possible overload ambiguities, I would use something that must be called explicitly. In this case, I'd use operator!() (e.g., "if( !x )") or a named member function (e.g., "if( x.Ok() )") -- note that these two have opposite meanings as conditions, of course. HOWARD: > Sometimes the conversion operators convert unexpectedly. You >might use your class in a place it doesn't belong, but because it >automatically converts to something that fits, the compiler >won't tell you. It's not an error. You told it your class could >be a bool, or an int, or whatever. SUTTER: Indeed, which can cause errors (code that compiles but shouldn't) and ambiguities (code that doesn't compile but should). In general, unless you have an overriding reason to break this rule, just avoid writing conversion operators and non-explicit ctors. They very often end up causing problems.