I have a pure virtual class CF that denotes any continuous function in 1D it is defined a header file
// tools.h
class CF
{
public:
virtual double operator()(double x) const=0 ;
};
what I intend to do is to wrap a couple of simple functions using functors and have them defined inside tools.h. If I use anonymous classes, like:
// tools.h
class : public CF{
public:
virtual double operator()(double x) const { return 0.0; }
} zero;
then I run into trouble sine the class is redefined everywhere tools.h is included. gcc complains:
// tools.h included in main.cpp, foo.cpp, and bar.cpp
foo.o:(.bss+0x0): multiple definition of `zero'
main.o:(.bss+0x0): first defined here
bar.o:(.bss+0x0): multiple definition of `zero'
main.o:(.bss+0x0): first defined here
I figured I could fix this by doing something like:
// tools.h
class zero_: public CF{
public:
virtual double operator()(double x) const { return 0.0; }
};
const static zero_ zero;
but I don’t like it since zero_ is visible everywhere and I don’t need it to be! How can I fix this issue?
By not doing it. Just have
And use it with
One of the reasons to have functors over functions is their ability to carry and expose state through operations, your approach would throw that away for gaining 2 parenthesis in conciseness.