Originally Posted by
bar5037
are pointters necessary to use? My professor always rants in class about how they aren't necessary to use.
I am of the same opinion, actually. They can't ALWAYS be avoided, but sometimes they are the only way to accomplish something.
To avoid a pointer in this case, you can declare a generic base class which represents your functions:
Code:
class function_base
{
public:
virtual ~function_base() { }
virtual double operator()(double value) = 0;
};
Then derive from it to implement a specific function:
Code:
class cubic_function : public function_base
{
double operator()(double value)
{
return value * value * value;
}
};
Then you'd pass it to your bisection function as a parameter, through a reference (which allows the polymorphism to work):
Code:
double bisect(double left, double right, const function_base &func)
{
}
Then you call "func()" inside the bisect() routine. When you call bisect() itself, you pass an actual function object:
Code:
bisect(left_val, right_val, cubic_function());
No pointers anywhere there, but it does use runtime polymorphism (the virtuals).
There are a lot of "gotchas," such as the necessity of the "const" reference.