指向具有多个对象的成员函数的指针向量 C++
Vector of pointers to member functions with multiple objects c++
考虑以下代码:
class A
{
public:
void aFoo() {}
};
class B
{
public:
void bFoo() {}
};
class C
{
public:
void c1Foo() {}
void c2Foo() {}
};
无论代码架构如何,是否可以创建指向成员函数的指针向量,即使这些函数在多个 classes 中?
在这种情况下,继承不是解决方案,因为我们不知道我们要在一个class中使用多少个函数(class C 有两个函数)。但我们知道它们都有相同的原型。
不同类的成员函数有不同的类型。因此,为了拥有任何同类容器(如 std::vector
或 std::array
),你需要将它们包装在某种可能代表它们的值类型中(如 boost::variant
或 boost::any
).
另一方面,如果您只需要特定类型的成员函数(例如 void()
)并且您不介意事先传递应该调用它们的对象,那么您可以只需将它们存储为 std::function<void()>
(对于此特定示例)并在将它们存储到容器中之前调用 std::bind
。
举个例子,给定:
A a; B b; C c;
std::vector<std::function<void()>> vector {
std::bind(&A::aFoo, a),
std::bind(&B::bFoo, b),
std::bind(&C::c1Foo, c),
std::bind(&C::c2Foo, c)
};
您可以致电:
for (auto fn : vector)
fn();
我不确定你想要实现什么,所以这可能不是很有帮助,但无论如何它就在这里。
正如其他人所说,您无法为此创建 std::vector,因为原型不同。但是,您可以像这样创建 std::tuple:
std::tuple<void (A::*)(), void (B::*)(), void (C::*)()> x(&A::aFoo, &B::bFoo, &C::c1Foo);
假设你有一个 class 的实例,比如 A a
那么你可以调用 (a.*std::get<0>(x))()
中的函数。
如果您也将对象存储在元组中,那么您可以迭代它们。下面的代码将做到这一点(假设你的系统中有 C++14 和 Boost)
#include <iostream>
#include <tuple>
#include <type_traits>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <functional>
class A
{
public:
void aFoo()
{
std::cout << "A" << std::endl;
}
};
class B
{
public:
void bFoo()
{
std::cout << "B" << std::endl;
}
};
class C
{
public:
void c1Foo()
{
std::cout << "C1" << std::endl;
}
void c2Foo() {}
};
// functor used by boost to iterate over the tuple
template <class Tuple>
struct functor
{
functor(Tuple t)
: m_tuple(t)
{
}
template <class X>
void operator()(X& x) const
{
using namespace boost::mpl;
using iter = typename find_if<Tuple, std::is_same < placeholders::_1, void (X::*)()> >::type;
using type = typename deref<iter>::type;
return (x.*std::get<type>(m_tuple))();
}
private:
Tuple m_tuple;
};
template <class Tuple>
functor<Tuple> make_functor(Tuple t)
{
return functor<Tuple>(t);
}
int main()
{
std::tuple<void (A::*)(), void (B::*)(), void (C::*)() > x(&A::aFoo, &B::bFoo, &C::c1Foo);
std::tuple<A, B, C> y;
boost::fusion::for_each(y, make_functor(x));
}
考虑以下代码:
class A
{
public:
void aFoo() {}
};
class B
{
public:
void bFoo() {}
};
class C
{
public:
void c1Foo() {}
void c2Foo() {}
};
无论代码架构如何,是否可以创建指向成员函数的指针向量,即使这些函数在多个 classes 中?
在这种情况下,继承不是解决方案,因为我们不知道我们要在一个class中使用多少个函数(class C 有两个函数)。但我们知道它们都有相同的原型。
不同类的成员函数有不同的类型。因此,为了拥有任何同类容器(如 std::vector
或 std::array
),你需要将它们包装在某种可能代表它们的值类型中(如 boost::variant
或 boost::any
).
另一方面,如果您只需要特定类型的成员函数(例如 void()
)并且您不介意事先传递应该调用它们的对象,那么您可以只需将它们存储为 std::function<void()>
(对于此特定示例)并在将它们存储到容器中之前调用 std::bind
。
举个例子,给定:
A a; B b; C c;
std::vector<std::function<void()>> vector {
std::bind(&A::aFoo, a),
std::bind(&B::bFoo, b),
std::bind(&C::c1Foo, c),
std::bind(&C::c2Foo, c)
};
您可以致电:
for (auto fn : vector)
fn();
我不确定你想要实现什么,所以这可能不是很有帮助,但无论如何它就在这里。
正如其他人所说,您无法为此创建 std::vector,因为原型不同。但是,您可以像这样创建 std::tuple:
std::tuple<void (A::*)(), void (B::*)(), void (C::*)()> x(&A::aFoo, &B::bFoo, &C::c1Foo);
假设你有一个 class 的实例,比如 A a
那么你可以调用 (a.*std::get<0>(x))()
中的函数。
如果您也将对象存储在元组中,那么您可以迭代它们。下面的代码将做到这一点(假设你的系统中有 C++14 和 Boost)
#include <iostream>
#include <tuple>
#include <type_traits>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <functional>
class A
{
public:
void aFoo()
{
std::cout << "A" << std::endl;
}
};
class B
{
public:
void bFoo()
{
std::cout << "B" << std::endl;
}
};
class C
{
public:
void c1Foo()
{
std::cout << "C1" << std::endl;
}
void c2Foo() {}
};
// functor used by boost to iterate over the tuple
template <class Tuple>
struct functor
{
functor(Tuple t)
: m_tuple(t)
{
}
template <class X>
void operator()(X& x) const
{
using namespace boost::mpl;
using iter = typename find_if<Tuple, std::is_same < placeholders::_1, void (X::*)()> >::type;
using type = typename deref<iter>::type;
return (x.*std::get<type>(m_tuple))();
}
private:
Tuple m_tuple;
};
template <class Tuple>
functor<Tuple> make_functor(Tuple t)
{
return functor<Tuple>(t);
}
int main()
{
std::tuple<void (A::*)(), void (B::*)(), void (C::*)() > x(&A::aFoo, &B::bFoo, &C::c1Foo);
std::tuple<A, B, C> y;
boost::fusion::for_each(y, make_functor(x));
}