簡體   English   中英

boost :: bind,boost :: shared_ptr和繼承

[英]boost::bind, boost::shared_ptr and inheritance

我是Boost庫的新手,而我的問題也有點復雜。 我試圖用上一個問題中找到的適合我的問題的例子來重新制定它。 (上一個問題在這里

#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調用自己的受保護方法。 在真正的問題中,boost :: bind為異步操作生成了一個回調方法,這就是為什么(我認為)我需要一個shared_ptr的原因。 否則,使用指針而不是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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM