Boost共享指针初始化 [英] Boost shared pointer initialization
问题描述
我是C ++的初学者。我有以下问题。在MevisPatientModel类中定义:
I am pretty a beginner in C++. I have following problem. In the class MevisPatientModel I defined:
typedef boost::shared_ptr<egMevisPatientModel> Ptr_t;
Then I "defined" the variable:
egMevisPatientModel::Ptr_t v_PatientModel;
现在,当我尝试访问MevisPatientModel类的getType函数时:
Now when I try to access the getType function of the class MevisPatientModel :
v_PatientModel->getType()
...我收到以下讯息:
...I'm getting following message:
声明失败! ... shared_ptr.hpp表达式px!= 0
Assertion failed! ...shared_ptr.hpp Expression px!= 0
我的意图是指针未初始化。现在我知道,这里有很多答案,适合我的问题。但正如我所说,我是一个初学者。说实话我不是这些答案明白。请帮助我,给我一个明确和具体的答案我的问题。非常感谢。
My intention is that the pointer wasn't initialized. Now I know that here are many answers which would "fit" to my question. But as I said, I am a beginner. To be honest I don't these answers understand. Please help me and give me a clear and specific answer to my problem. Thank you very much.
Greets,Marco
Greets, Marco
推荐答案
使用 new
创建动态对象,并将生成的指针分配给 shared_ptr
:
You need to create an dynamic object with new
and assign the resulting pointer into the shared_ptr
:
egMevisPatientModel::Ptr_t v_PatientModel(
std::make_shared<egMevisPatientModel>());
这篇关于Boost共享指针初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!