One last thing if you don't mind:
How costly is calling the position function now in comparison to what I was doing before with function pointers?
1 2 3 4 5 6 7 8 9
|
// what I was doing before which worked with the circle function
Coordinate Movable::position(double t)
{
return mpPath(t);
}
void Movable::setPath(Coordinate(*pPath)(double))
{
mpPath = pPath;
}
|
In working code, I imagine that my setPath() function would only be called a limited number of times to set things up, but my position() function would be called
many times at small increments of t.
Is there a lot of overhead in using the lambda with a function object? I ask out of ignorance. I guess I could do some time tests to compare calling the position of the path as a circle parametrization vs the Line parametrization.
Edit: I did some really basic time tests to compare use of circle function in both my code and yours, and it seems that the function pointer code runs faster than the code with the std::function and lambda, is this what I should expect?
_______________________________
Edit side note:
Also I figured out a good way to make it so your code works with both pure functions and class member stuff
1 2 3 4 5 6 7 8 9 10
|
template<typename T>
void setPath(const T& obj)
{
mpPath = obj.get_path();
}
void setPath(Coordinate(*pPath)(double))
{
mpPath = pPath;
}
|
Yay for function overloading, now I can do this.
1 2 3 4 5 6 7 8 9
|
Coordinate circle(double t)
{
return Coordinate(std::cos(t),
std::sin(t));
}
//...main
object2.setPath(my_line);
object2.setPath(circle);
std::cout << object2.position(0.5)
|