On Tue, Dec 1, 2009 at 1:08 PM, Kirk, Benjamin (JSC-EG311)
<[email protected]> wrote:
> there are a number of places where we use function pointers, which seems
> like a pretty C-ish way to do a lot of things.
>
> Take, for example, the ExactSolution class... The user provides a function
> pointer which is stored in _exact_value function pointer.
>
> What if instead we declare a virtual function
> ExactSolution::exact_value(...) with the same interface as the function
> pointer. The default behavior would the then to just call the function
> pointer inside the exact_value() method, but as an alternative the user
> could instead override exact_value() in a derived class?
Sounds cool. If you don't want to force someone to derive from
ExactSolution explicitly, you could also do something like:
#include <iostream>
class A
{
public:
template <class F>
void f(F& f_obj)
{
f_obj.f();
}
};
// Not derived from A
class B
{
public:
// Implements a f() function
void f()
{
std::cout << "B::f()" << std::endl;
}
};
// Not derived from A
class C
{
public:
// Implements a f() function
void f()
{
std::cout << "C::f()" << std::endl;
}
};
int main()
{
A a;
B b;
C c;
a.f(b);
a.f(c);
return 0;
}
Prints:
B::f()
C::f()
Here, A::f is like the ExactSolution::exact_value(...) function and
classes B and C are user-defined classes which only have to implement
a function "f()" with the same name and proper calling syntax.
Drawback is you have to pass the user object whenever you call
exact_value(), but maybe that's not a big deal.
--
John
------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing.
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Libmesh-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libmesh-devel