C++ で変換コンストラクターを試すためだけに単純なクラスを作成しました。
動作しているように見えますが、特定の操作を行うと、コンパイラがそれを呼び出していないようです。理由、またはおそらくどこが間違っているかがわかります。
#include <iostream>
using std::cout; using std::endl;
class CostruttoreConversione
{
public:
CostruttoreConversione(int val = 0)
{
cout << "Costruttore chiamato" << endl;
valore = val;
}
inline int getValore(){
return valore;
}
CostruttoreConversione provaConversioneImplicita()
{
return -10; //here the type *should* be converted; doesn't happen.
}
private:
int valore;
};
int main(void){
CostruttoreConversione obj(10);
cout << "obj = " << obj.getValore() << endl;
obj = 20; //WORKS
cout << obj.getValore() << endl;
// cout << obj.provaConversioneImplicita() << endl; doesn't work.
return 0;
}