I want to use operator overloading template function to
produce automatic type conversion.
When I was compiling the following code, I got an error:
cannot convert parameter 1 from 'Four' to 'Three'. but If
I use general member function(commented code) instead of
template function, it works well.
Would you please tell me what is the problem? thanks.
//------------- code --------------
#include <string>
#include <iostream>
using namespace std;
class Three {
int i;
public:
Three(int ii = 0, int = 0) : i(ii) { }
Quote:
};
class Four {
int x;
public:
Four(int xx) : x( xx) { }
template<class T> operator T(){
return T(x);
}
//operator Three() const {
// return Three(x);
//}
Quote:
};
void g(Three) { }
int main(int argc, char* argv[])
{
Four four(1) ;
g(four) ;
return 0;
Quote:
}