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?

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.