これは、私が本番環境で実行しているコードのミニチュア バージョンです。私の実際のコードは、gcc コンパイラーと Intel コンパイラーの下で異なる動作をすることがわかりました。私の最善の推測は、未定義の動作です。次のサンプル コードを検討してください。
#include <iostream>
struct baseFunctor{
virtual double operator()(double x) const = 0;
};
class mathObj{
const baseFunctor *tmp1, *tmp2;
public:
void set(const baseFunctor& b1, const baseFunctor& b2){
tmp1 = &b1;
tmp2 = &b2;
}
double getValue(double x){
return (tmp1->operator()(x) + tmp2->operator()(x));
}
};
int main () {
mathObj obj;
struct squareFunctor: public baseFunctor {
double operator()(double x) const { return x*x; }
};
struct cubeFunctor: public baseFunctor {
double operator()(double x) const { return x*x*x; }
};
obj.set(squareFunctor(), cubeFunctor());
std::cout << obj.getValue(10) << std::endl;
return 0;
}
未定義obj.set(squareFunctor(), cubeFunctor());
の動作を引き起こす可能性はありますか?