To my understanding, certain classes, like ifstream, have a conversion to a primitive data type, like bool. In other words, you can do this:

if((fin >> x) && done == false)
//...

Now, does this simply work because ifstream has the && operator overloaded, or can objects of that class just always be converted to bool? So would this:

if((fin >> x))
//...

work? If so, how can you set a class to convert to bool, or int, or any other data type?

Dani AI

Generated

Short answer: streams convert to a Boolean-like value; there is no special && overload on ifstream. As pointed out, fin >> x returns a reference to the stream, and the stream class provides a conversion so that the expression can be tested in if. Historically that was implemented as an operator void*-style trick; since C++11 standard library streams expose an explicit operator bool() that checks the stream state (see basic_ios::operator bool).

For your own classes you can provide a user-defined conversion function with the operator T() syntax; see the language reference on cast operators for details (cast_operator). A safer modern pattern for boolean contexts is to declare an explicit conversion to bool so the object can be used in if(obj) but won’t silently convert to integers or pointers:

struct MyStreamLike {
    bool good() const;
    explicit operator bool() const { return good(); }
};

Notes and cautions:

  • Prefer explicit operator bool() (C++11+) to avoid accidental numeric conversions. Before C++11 you needed the “safe-bool” idiom to avoid unwanted conversions.
  • Do not rely on overloading operator&& to get short-circuit logical behavior for user types; overloaded operator&& does not provide the same short-circuit semantics as the built-in operator in mixed expressions.
  • For streams use if (fin >> x) — it’s the idiomatic way to test whether extraction succeeded (internally it tests stream state flags such as failbit).

This expands on ’s conversion examples and clarifies why if ((fin >> x) && done == false) works: the stream result is converted to bool, then the built-in && combines that boolean with the right-hand side.

Recommended Answers

All 3 Replies

1) the overloaded operator << return a reference to the fin object
2) type casting to primitive data type is as follows.

class complex
{
       int r, i ;
public :
       operator int()
       {
           return r*r-i*i ; // some conversion to int
       }
       complex():r(0), i(0) { }
       complex ( int a, int b ) { r = a; i = b ; }
      
};

if both the source & destination are user defined then the function for the cast may be either in source ot in destination.
Conversion routine in source is similar to that of the primitive types. But in destination is a little different.

class dmy // source
{
    int day, mth, yr ;
 //methods
} ;
class date // destination
{
    char a[10] ;
// conversion routine
   public:
    date ( dmy t ){/*convert ints to char array*/}
};

Hope this helps:)

Err... not exactly sure what you were talking about with the source and destination stuff, but this:

operator int()
{
return r*r-i*i ; // some conversion to int
}

is what I was looking for. Thanks.

lets say there are 2 classes date & dmy then
date a = new dmy() ;
needs a conversion.
this conversion routine may be in source(dmy) or in destination(date).
if it is in source(dmy) all you have to do is
operator date() {... }
if the routine has to be in destination then the constructor should be used
date(dmy t) {... }
I don't know how it works but it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.