R
Ryan Mitchley
Hi all
Given
bool bResult;
shared_ptr<cSampleData> pNewData;
shared_ptr<cBase> pNewBase;
where cSampleData is descended from cBase, the following gives me a valid
pNewData to the correct type:
bResult = Input(INPORT_snapshot)->ReceiveNow(pNewBase);
pNewData = pNewBase;
This works fine, although the assignment conversion seems to be a bit
longwinded.
ReceiveNow is defined as:
bool ReceiveNow(shared_ptr<CBase> & pNewObject);
However, the compiler won't allow me to call the following, due to the
inability to initialize the reference parameter of ReceiveNow:
bResult = Input(INPORT_snapshot)->ReceiveNow(pNewData);
Adding a cast compiles correctly:
bResult =
Input(INPORT_snapshot)->ReceiveNow(boost::dynamic_pointer_cast<CBase>(pNewDa
ta));
but the pNewData is null afterwards.
Is there any way of passing the pointer to the derived type (pNewData) to
the function expecting a base class pointer in a single function call?
Is the shared_ptr getting confused by the dynamic_pointer_cast and deleting
the object incorrectly? Do I need to use a weak_ptr?
Any suggestions?
Thanks!
Ryan
Given
bool bResult;
shared_ptr<cSampleData> pNewData;
shared_ptr<cBase> pNewBase;
where cSampleData is descended from cBase, the following gives me a valid
pNewData to the correct type:
bResult = Input(INPORT_snapshot)->ReceiveNow(pNewBase);
pNewData = pNewBase;
This works fine, although the assignment conversion seems to be a bit
longwinded.
ReceiveNow is defined as:
bool ReceiveNow(shared_ptr<CBase> & pNewObject);
However, the compiler won't allow me to call the following, due to the
inability to initialize the reference parameter of ReceiveNow:
bResult = Input(INPORT_snapshot)->ReceiveNow(pNewData);
Adding a cast compiles correctly:
bResult =
Input(INPORT_snapshot)->ReceiveNow(boost::dynamic_pointer_cast<CBase>(pNewDa
ta));
but the pNewData is null afterwards.
Is there any way of passing the pointer to the derived type (pNewData) to
the function expecting a base class pointer in a single function call?
Is the shared_ptr getting confused by the dynamic_pointer_cast and deleting
the object incorrectly? Do I need to use a weak_ptr?
Any suggestions?
Thanks!
Ryan