I was looking forward to eliminating the ambiguity between the normal constructor and the automatic conversion constructor.
as far as I know, this ambiguity can be partially removed by declaring the normal constructor as explicit, so compilers avoid using this constructor as a conversion constructor, for example:
#include <iostream>
#include <fstream>
class Integer{
int i ;
public:
explicit Integer (const int _i) : i(_i) {}
Integer (const int& ii ) : i (ii) {}
operator int() {return int (i) ;}
friend std::ostream & operator <<(std::ostream &os, const Integer io ) {
std::cout << io.i ;
return os ;
}
};
int main() {
Integer io2 = 20 ;
std:: cout << io2 << "\n" ;
int i = io2 ;
std:: cout << i << "\n" ;
}
conclusion:
20
20
my questions:
1- Is there a standard way to force a constructor to be used as a transform constructor, since there is a standard way to force use as a regular constructor?
2 - , , , , ?