I beleive you have to return the value from _f in function f(), so f should look like this:
int f(const int& a, const int& b) { return (this->*_f)(a,b); } i have no idea why g++ doesn't give warning for omitting the return statement. That's why you see in c++ a lot of main() function without return statement although they are supposed to return int. To force a warning use "g++ -Wall yourFile.cpp -o output", then it should gives something similar to this: yourFile.cpp: In member function ‘int Calc2::f(const int&, const int&)’: yourFile.cpp:17: warning: no return statement in function returning non-void yourFile.cpp:17: warning: control reaches end of non-void function NOTE: you still don't have to return something from main, but as a good practice you should return the program exit status so you can use it in a pipe or some other chain of commands. On Tue, 2006-10-17 at 09:30 -0700, angelo wrote: > Hello, I am new to this group so hello to everybody. > I start with a problem which occur when the optimization > flag is enabled (-O1, ..., -O3) when compiling the following > code which contain function pointers. The code compile > with no problems and works correctly when no > optimization is selected. When I turn-on the optimizer, > the program produces a non-correct result. I used gcc > 4.0.3 under Linux Ubuntu. > Thanks in advance for any help or suggestion. > > #include <iostream> > //--------------------------------------------------------------------------- > // Calc2 use a function pointer initialized at the constructor to point > to > // the selected operation. > // + No need for inheritance > // - Indirection of pointer, hard syntax > //--------------------------------------------------------------------------- > class Calc2 { > public: > Calc2(const char op) { > switch(op) { > case '+' : _f = &Calc2::_add; break; > case '*' : _f = &Calc2::_mul; break; > case '/' : _f = &Calc2::_div; break; > } > } > int f(const int& a, const int& b) { (this->*_f)(a,b); } > private: > int (Calc2::*_f)(const int& a, const int& b); > int _add(const int& a, const int& b) { return (a + b); } > int _mul(const int& a, const int& b) { return (a * b); } > int _div(const int& a, const int& b) { return (a / b); } > }; > > // Main program > int main() > { > Calc2 calc2('+'); > std::cout << calc2.f(6, 2) << std::endl; > } > > _______________________________________________ > help-gplusplus mailing list > help-gplusplus@gnu.org > http://lists.gnu.org/mailman/listinfo/help-gplusplus -- John V. Shahid _______________________________________________ help-gplusplus mailing list help-gplusplus@gnu.org http://lists.gnu.org/mailman/listinfo/help-gplusplus