How to initialize shared pointers in constructor?

282 Views Asked by At

I am trying to initialize objects from other classes in my constructor as shared pointers. I need a shred pointer because I need a reference to use in another method in ...

header

class MyClass
{
public:
   MyClass() ;
   ~MyClass() {};
   void myMethod();
private:
   std::shared_ptr<dds::pub::Publisher>m_pub;
   std::shared_ptr<dds::domain::DomainParticipant>m_part;
};

cpp

MyClass::MyClass()  
{
  m_part = std::make_shared<dds::domain::DomainParticipant>(domain::default_id());
  m_pub = std::make_shared<dds::pub::Publisher>(m_part);
}
MyClass::myMethod()
{
  //m_part, m_pub are used here 
}

what am I missing here?

Error   C2039   'delegate': is not a member of 'std::shared_ptr<dds::domain::DomainParticipant>'     

dds::pub::Publisher

namespace dds
{
  namespace pub
  {
     typedef dds::pub::detail::Publisher Publisher;
  }
}

Publisher

namespace dds { namespace pub { namespace detail {
typedef 
dds::pub::TPublisher<org::eclipse::cyclonedds::pub::PublisherDelegate> Publisher;
} } }

PublisherDelegate

namespace dds { namespace pub { namespace detail {
    typedef 
dds::pub::TPublisher<org::eclipse::cyclonedds::pub::PublisherDelegate> Publisher;
} } }


class OMG_DDS_API PublisherDelegate : public 
org::eclipse::cyclonedds::core::EntityDelegate
{
public:
typedef ::dds::core::smart_ptr_traits< PublisherDelegate >::ref_type ref_type;
typedef ::dds::core::smart_ptr_traits< PublisherDelegate >::weak_ref_type weak_ref_type;

PublisherDelegate(const dds::domain::DomainParticipant& dp,
                  const dds::pub::qos::PublisherQos& qos,
                  dds::pub::PublisherListener* listener,
                  const dds::core::status::StatusMask& event_mask);

TPublisher

template <typename DELEGATE>
class dds::pub::TPublisher : public dds::core::TEntity<DELEGATE>
{
public:

   typedef dds::pub::PublisherListener                 Listener;

public:
   OMG_DDS_REF_TYPE_PROTECTED_DC(TPublisher, dds::core::TEntity, DELEGATE)
   OMG_DDS_IMPLICIT_REF_BASE(TPublisher)

   TPublisher(const dds::domain::DomainParticipant& dp);

   TPublisher(const dds::domain::DomainParticipant& dp,
           const dds::pub::qos::PublisherQos& qos,
           dds::pub::PublisherListener* listener = NULL,
           const dds::core::status::StatusMask& mask = dds::core::status::StatusMask::none());

I tried the method given in answer got new error,

Error   C2672   'std::dynamic_pointer_cast': no matching overloaded function  in TPublisher.hpp   
1

There are 1 best solutions below

4
273K On

I guess m_pub should be initialised like this

m_pub = std::make_shared<dds::pub::Publisher>(*m_part);

The class dds::pub::Publisher a.k.a. dds::pub::TPublisher has the constructor taking const dds::domain::DomainParticipant by reference.

The answer is changed after the question has been updated.