boost::shared_ptr assignment...

Started by
7 comments, last by Brother Bob 11 years, 7 months ago

vector<shared_ptr<D3DXMATRIX>> boneMatrixPtrs;

if(b != NULL)boneMesh->boneMatrixPtrs.push_back(new shared_ptr<D3DXMATRIX>(&b->combinedTransform));


boneMatrixPtrs are linked to combinedtransform
and combined with offset transform later on.
Wish you would know what I am talking about.
This just doesn't work

[Translated]
error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : Cannot convert the variable from 'std::tr1::shared_ptr<_Ty> *' to 'std::tr1::shared_ptr<_Ty> &&' q:\perfectsim 24-8-2012 [test2]\perfectsim v4.0\perfectsim v4.0\cmesh.cpp 384 1 PerfectSim V4.0
Advertisement
The vector stores shared_prt but you're passing it a pointer to a shared_ptr. To fix the error itself you need to drop the new because the vector doesn't store pointers to the smart pointer; but more importantly, is there a reason you're storing trivial objects such a matrices in a shared pointer in the first place?
new shared_ptr<D3DXMATRIX> returns a pointer to a shared_ptr. If you just want to put a shared_ptr into the vector, you should be able to just get rid of the 'new' (if my understanding of C++ constructors is still intact).
Thanks you guys.
Bob: I want to free up from the hassle of managing raw pointers. Just lazy :)
Let me rephrase the question; why are you storing matrices by any kind of pointer and not just directly by value?
As far as I know, boneMatrixPtrs have to be altered when combinedTransforms are altered.
So they are linked. I think that's why
I can't count how many ways we need a pointer
1) Change when the parent pointer changes
2) Size too large by value
3) contains an array of something
That's all?
In case I use a shared_ptr, do I eliminate its destructor?
Thanks
Jack
I see some problems with your motivation for storing pointers (which in itself is not wrong, I accept your reason) and the fact that it stores smart pointers. Is combinedTransforms a value of type D3DXMATRIX? Is the idea to take the address of that matrix and put it in a vector so that you can keep a vector of pointers that are linked to the actual combined transforms stored somewhere else?

Assuming that is the case, you're going to have a problem: the shared pointers stored in the vector will automatically release the pointer they hold when the vector is released, but since you're storing pointer to values the pointers must not be released. The lifetime of the objects being pointed to is managed by the object holding the original matrices, and the ownership cannot be transferred to the smart pointers.

Assuming, again, that this is the case, you actually want plain pointers in the vector instead of smart pointers. Since you don't have ownership or lifetime management problems with the pointers in the vector, you won't have any problems with just letting the vector release itself.

This topic is closed to new replies.

Advertisement