【问题标题】:Boost shared_ptr on class object在类对象上提升 shared_ptr
【发布时间】:2016-12-14 19:28:48
【问题描述】:

假设我有以下代码:

controller.hpp

#include "testing.hpp"
#include <boost/shared_ptr.hpp>
class controller
{
    public:
        controller(void);
        void test_func (void);
        boost::shared_ptr <testing> _testing;
}

控制器.cpp

#include "controller.hpp"
controller::controller() {
    boost::shared_ptr <testing> _testing (new testing);
    std::cout << _testing->test_bool << std::endl;
}

void controller::test_func (void) {
    // how to use _testing object?
    std::cout << _testing->test_bool << std::endl;

    return;
}

int main (void) {
    controller _controller;    // constructor called
    test_func();
    return 0;
}

测试.hpp

class testing
{
    public:
        bool test_bool = true;
}

我在这里为班级成员正确使用shared_ptr 吗? controller 类中的多个函数需要使用_testing 对象,并且我不希望每次指针超出范围时都调用testing 类的构造函数/解构函数。也许这是无法避免的,我开始意识到。

【问题讨论】:

  • 我会使用boost::make_shared 而不是new.get() 对您的使用方式毫无意义。此外,main() 中没有名为 _testing 的变量,因此无法编译。
  • 好的。你说得对。我正在尝试将_testing 共享给controller 类中的所有函数,而不是每次都调用它的构造函数。

标签: c++ class boost shared-ptr


【解决方案1】:

测试对象在控制器构造函数中被构造并在超出范围时被销毁。

只是:

int main (void) {
    controller _controller;    // constructor called
    _controller.test_func(); 
    // destructor of controller called, because it go out of scope,
    // so testing destructor is called too because, there is no more
    // shared_ptr pointing to it!
}

[EDITED] 匹配问题所有者的编辑

【讨论】:

    【解决方案2】:

    我冒昧地重写了您的代码来演示共享指针的用法。非常典型地使用它,以便一个对象可以同时在两个地方,四处移动,并且自动销毁。

    #include <iostream>
    #include <boost/shared_ptr.hpp>
    #include <boost/make_shared.hpp>
    
    class testing
    {
    public:
        std::string str;
        testing( const char* in ) : str( in ) { }
    };
    typedef boost::shared_ptr <testing> SP_testing;
    
    class controller
    {
    public:
        controller( const char* in );
        void test_func ( );
        SP_testing _testing;
    };
    
    controller::controller( const char* in )
        :_testing( boost::make_shared< testing >( in ) )
    {
        std::cout << "controller constructor: \"" << _testing->str << '\"' << std::endl;
    }
    
    void controller::test_func (void) {
        std::cout << "test_func: \"" << _testing->str << "\"  - cnt: " << _testing.use_count( ) << std::endl;
    }
    
    int main (void)
    {
        //yet to be used shared pointer
        SP_testing outsider;
        {
            //this will create an instance of testing.
            controller _controller( "this is a test" ); // constructor called, prints
            outsider= _controller._testing;             //assign shared pointer
            _controller.test_func( );                   // test called, prints usage count.
    
        }//leaving scope, _controller will be destroyed but the _testing it created will not
    
        std::cout << "outsider: \"" << outsider->str << "\"  - cnt: " << outsider.use_count( ) << std::endl;
    
        //now testing will get destroyed.
        return 0;
    }
    

    在上面,“局外人”将指针指向controller::_testing。在test_func 他们都有一个指向同一个对象的指针。即使控制器创建了测试对象,当控制器被销毁时它也不会被销毁。出现这种情况时非常方便。此代码可以粘贴到一个 .cpp 文件中。感谢@Dan Mašek 提醒make_shared

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      相关资源
      最近更新 更多