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

标签 c++ inheritance boost shared-ptr boost-bind

我是 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/

相关文章:

c++ - 数组大小是否需要为常量?

c++ - 具有 move 构造函数和赋值的类的默认复制构造函数和赋值

c++ - C++ 中的伯努利随机变量

c++ - C++中的继承和指针

java - 你能得到继承的方法的子类吗?

c++ - 在多个源文件中拆分代码,现在出现数组错误

c++ - 模板实例化不 "do inheritance"

c++ - 如何从 boost::timer::cpu_timer 获取耗时(以秒为单位)?

c++ - Boost C++ 库链接器错误 libboost_serialization-vc100-mt-gd-1_47.lib

multithreading - 为什么 C++0x 标准委员会拒绝了 boost::shared_mutex?