gpt4 book ai didi

c++ - boost::bind、boost::shared_ptr 和继承

转载 作者:太空宇宙 更新时间:2023-11-04 12:16:49 28 4
gpt4 key购买 nike

我是 Boost 库的新手,我遇到了一个对我来说有点复杂的问题。我试图用在上一个问题中发现的一个可能很适合我的问题的例子来重新表述它。(上一题是here)

#include <boost/bind.hpp>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

class Base
: public boost::enable_shared_from_this<Base>,
private boost::noncopyable
{
public:
virtual void test() = 0;

protected:
virtual void foo(int i) = 0;
};

class Derived
: public Base
{
protected:
void foo(int i)
{ std::cout << "Base: " << i << std::endl; }

std::map<int, int> data;

public:

Derived()
{
data[0] = 5;
data[1] = 6;
data[2] = 7;
}

void test()
{
std::for_each(data.begin(), data.end(),
boost::bind(&Derived::foo, shared_from_this(),
boost::bind(&std::map<int, int>::value_type::second, _1)));
}
};

typedef boost::shared_ptr<Base> Base_ptr;

int main(int, const char**)
{

std::set<Base_ptr> Bases_;
Base_ptr derived(new Derived());

Bases_.insert(derived);
derived->test();


return 0;
}

我有一个包含在集合中的基础对象,以及不同的派生对象(在这个例子中,只有一个)。派生对象应该使用 boost::bind 调用他自己的 protected 方法。在实际问题中,boost::bind 为异步操作生成回调方法,这就是(我认为)我需要 shared_ptr 的原因。否则,使用指针 this 而不是 shared_from_this() 可以解决问题。

当我编译这段代码时,我收到一条很长的错误消息,结尾是(我认为这是最重要的部分):

bind_test.cpp:43:78:   instantiated from here
/usr/include/boost/bind/mem_fn_template.hpp:156:53: error: pointer to member type ‘void (Derived::)(int)’ incompatible with object type ‘Base’
/usr/include/boost/bind/mem_fn_template.hpp:156:53: error: return-statement with a value, in function returning 'void'

我尝试使用更多来自 enable_shared_from_this 的继承和一些静态转换来管理:

#include <boost/bind.hpp>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

class Base
: public boost::enable_shared_from_this<Base>,
private boost::noncopyable
{
public:
virtual void test() = 0;

protected:
virtual void foo(int i) = 0;
};

class Derived
: public boost::enable_shared_from_this<Derived>,
public Base
{
protected:
void foo(int i)
{ std::cout << "Base: " << i << std::endl; }

std::map<int, int> data;

public:

Derived()
{
data[0] = 5;
data[1] = 6;
data[2] = 7;
}

void test()
{
std::for_each(data.begin(), data.end(),
boost::bind(&Derived::foo, boost::enable_shared_from_this<Derived>::shared_from_this(),
boost::bind(&std::map<int, int>::value_type::second, _1)));
}
};

typedef boost::shared_ptr<Base> Base_ptr;

int main(int, const char**)
{

std::set<Base_ptr> Bases_;
Base_ptr derived(new Derived());

Bases_.insert(derived);
derived->test();


return 0;
}

但是我在运行时遇到了一个错误:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
what(): tr1::bad_weak_ptr

有人可能知道如何管理它吗?谢谢。

艾蒂安。

最佳答案

它适用于此解决方法,但我对此并不满意,所以如果有人找到更好的解决方案,请继续。

#include <boost/bind.hpp>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

class Base
: public boost::enable_shared_from_this<Base>,
private boost::noncopyable
{
public:
virtual void test() = 0;

//protected:
virtual void foo(int i) = 0;
};

class Derived
: public Base
{
protected:
void foo(int i)
{ std::cout << "Base: " << i << std::endl; }

std::map<int, int> data;

public:

Derived()
{
data[0] = 5;
data[1] = 6;
data[2] = 7;
}

void test()
{
std::for_each(data.begin(), data.end(),
boost::bind(&Base::foo, shared_from_this(),
boost::bind(&std::map<int, int>::value_type::second, _1)));
}
};

typedef boost::shared_ptr<Base> Base_ptr;

int main(int, const char**)
{

std::set<Base_ptr> Bases_;
Base_ptr derived(new Derived());

Bases_.insert(derived);
derived->test();


return 0;
}

关于c++ - boost::bind、boost::shared_ptr 和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7340830/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com