ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
#include <iostream>
struct functor_base
{
virtual void operator()() const = 0;
};
struct functor1 : functor_base
{
virtual void operator()() const
{
std::cout << "functor1" << std::endl;
}
};
struct functor2 : functor_base
{
virtual void operator()() const
{
std::cout << "functor2" << std::endl;
}
};
void foo(functor_base & functor)
{
// No if's or function pointer.
functor();
}
int main()
{
foo(functor1());
foo(functor2());
}
#include <iostream>
struct functor1
{
virtual void operator()() const
{
std::cout << "functor1" << std::endl;
}
};
struct functor2
{
virtual void operator()() const
{
std::cout << "functor2" << std::endl;
}
};
template <typename funcT>
void foo(funcT & functor)
{
// No if's or function pointer.
functor();
}
int main()
{
foo(functor1());
foo(functor2());
}
#include <iostream>
struct functor1
{
void operator()() const
{
std::cout << "functor1" << std::endl;
}
};
struct functor2
{
void operator()() const
{
std::cout << "functor2" << std::endl;
}
};
template <typename funcT>
void foo(funcT & functor)
{
// No if's or function pointer.
functor();
}
int main()
{
foo(functor1());
foo(functor2());
}
ASKER
C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.
TRUSTED BY
Um, it really depends on what you are trying to do. In C++ it is preferable to use functors rather than function pointers.
http://www.newty.de/fpt/functor.html#chapter4
Open in new window