C++ 类对象上的Boost共享\u ptr

C++ 类对象上的Boost共享\u ptr,c++,class,boost,shared-ptr,C++,Class,Boost,Shared Ptr,假设我有以下代码: 控制器.hpp #include "testing.hpp" #include <boost/shared_ptr.hpp> class controller { public: controller(void); void test_func (void); boost::shared_ptr <testing> _testing; } 我是否在这里为班级成员正确使用了共享\u ptr?类co

假设我有以下代码:

控制器.hpp

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

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

测试对象在控制器构造函数中构造,在超出范围时被破坏

只是:


[已编辑]为匹配问题所有者编辑,测试对象在控制器构造函数中构造,并在超出范围时销毁

只是:


[已编辑]为了匹配问题所有者的编辑

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

#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;
}
#包括
#包括
#包括
类测试
{
公众:
std::字符串str;
测试(const char*in):str(in){
};
typedef boost::共享的ptr SP_测试;
类控制器
{
公众:
控制器(const char*in);
无效测试_func();
SP_测试——SP_测试;
};
控制器::控制器(常量字符*in)
:_测试(boost::使_共享(in))
{

std::cout我冒昧地重写了您的代码,以演示共享指针的用法。非常典型的情况是,使用共享指针可以使对象同时在两个位置移动,并且自动销毁

#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;
}
#包括
#包括
#包括
类测试
{
公众:
std::字符串str;
测试(const char*in):str(in){
};
typedef boost::共享的ptr SP_测试;
类控制器
{
公众:
控制器(const char*in);
无效测试_func();
SP_测试——SP_测试;
};
控制器::控制器(常量字符*in)
:_测试(boost::使_共享(in))
{

std::我可以使用
boost::make_shared
而不是
new
.get()
的使用方式是毫无意义的。此外,在
main()中没有名为
\u testing
的变量
,这样就不会编译了。好吧。你说得对。我正在尝试将
\u测试
共享给类
控制器
中的所有函数,而不每次调用它的构造函数。我会使用
boost::make\u shared
而不是
new
。The
.get()
的使用方式毫无意义。此外,在
main()
中没有名为
\u testing
的变量,因此无法编译。好的。你说得对。我正在尝试将
\u testing
共享给类
controller
中的所有函数,而不是每次调用它的构造函数。
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!
}
#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;
}