For the code below, I would like to know how to set std::shared_ptr
to point the given objects in the two member functions. The Vector3
object which is allocated in the main function is not going to be deleted until the end of the program.
#include <memory>
#include <vector>
using std::vector;
using std::shared_ptr;
class Vector3
{
// ...
};
class Face
{
vector < shared_ptr <Vector3> > vtx;
public:
void addVtx (const Vector3& vt)
{
// vtx.push_back (); <-- how to push_back ?
}
void setVtx (const int v, const Vector3& vt)
{
if ( vtx.size() > 0 && v < vtx.size() )
{
// vtx[v] = &vt; <-- how to assign ?
}
}
};
int main ()
{
vector <Vector3> vec (3);
Face face;
face.addVtx (vec[0]);
face.setVtx (0, vec[0])
return 0;
}
A shared_ptr can share ownership of an object while storing a pointer to another object. This feature can be used to point to member objects while owning the object they belong to. The stored pointer is the one accessed by get(), the dereference and the comparison operators.
A null shared_ptr does serve the same purpose as a raw null pointer. It might indicate the non-availability of data. However, for the most part, there is no reason for a null shared_ptr to possess a control block or a managed nullptr .
The shared_ptr type is a smart pointer in the C++ standard library that is designed for scenarios in which more than one owner might have to manage the lifetime of the object in memory.
Use unique_ptr when you want to have single ownership(Exclusive) of the resource. Only one unique_ptr can point to one resource. Since there can be one unique_ptr for single resource its not possible to copy one unique_ptr to another. A shared_ptr is a container for raw pointers.
There is not much point in using a shared_ptr
for an automatically allocated object.
Technically you can do it, by giving the shared_ptr
a deleter that doesn't do anything, and changing your vtx
to be a vector of shared pointers to const
vectors.
E.g. changing the declaration to
vector < shared_ptr <Vector3 const> > vtx;
and adding a pointer like this:
vtx.push_back( shared_ptr<Vector3 const>( &vt, [](Vector3 const*){} ) );
Disclaimer: untested code, not touched by a compiler.
However, unless you're going to also add pointers to vectors that need to be deleted, just use a vector of raw pointers. Or, just use a vector of vectors, and copy the in-data.
It's not a good idea to use raw pointers to hold ownership. For that use smart pointers.
But conversely, it's not a good idea to use ownership smart pointers to hold pure references to static or automatic objects. For that, use raw pointers or references.
In general.
Disregarding the argument about whether using shared_ptr
is a good idea or not, as explained by the accepted answer, you can use the following if you continue to use shared_ptr
:
void addVtx (const Vector3& vt)
{
vtx.push_back(std::make_shared<Vector3>(vt));
}
void setVtx (size_t v, const Vector3& vt)
{
if ( vtx.size() > 0 && v < vtx.size() )
{
vtx[v] = std::make_shared<Vector3>(vt);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With