Hi all
I need some explanation on understanding the concept of conversion operators.
I read about conversion operators and found that they automatically convert one object into another one.
Here is some code.
I am little bit confused in this.Code:
class A {
ifstream file;
string fileName;
public:
A(const string& fname)
: fileName(fname), file(fname.c_str()) {}
string name() const { return fileName; }
void name(const string& newName) {
fileName = newName;
}
operator ifstream&() { return file; }
};
int main() {
A a("A.cpp");
cout << a.name() << endl;
a.close(); // Compile time error. close() not member of A
}
Since automatic type conversion, converts object "a" to type of ifstream then it should be able to call close() function of ifstream. So why does it giving error.
I will very thankful if some one answered my following question:
1) HOW conversion operator work's, I mean to say how calls are gone to conversion operator?
2) What is the criteria to convert one object to another, is this rule is universal?
I have read some where about conversion operator:
What does it mean????Quote:
Automatic type conversion happens only in function calls, not during member selection
Thanks for your time.
Thanks
Nickman
Can