[odb-users] Re: Self persisting objects?

Boris Kolpackov boris at codesynthesis.com
Tue Sep 4 05:58:32 EDT 2012


Hi Oded,

In the future please keep your replies CC'ed to the odb-users mailing
list as discussed in the posting guidelines:

http://www.codesynthesis.com/support/posting-guidelines.xhtml

Oded Arbel <oded at geek.co.il> writes:

> On Fri, Aug 31, 2012 at 2:15 PM, Boris Kolpackov <boris at codesynthesis.com> wrote:
>
> > I mean your persistent class (Employee) uses a smart pointer as its
> > object pointer (normally specified with the 'pointer' pragma, the
> > --default-pointer ODB compiler option, or if you are using Boost
> > or Qt smart-pointer profiles). See Section 3.2, "Object and View
> > Pointers" for details.
> 
> Yes, I do use std::shared_ptr as the shared pointer for ODB objects.
> 
> > If your persistent class uses a smart pointer, say, shared_ptr, then
> > initializing it from a reference that was passed to persist() (which
> > is needed in order to enter this object into the session) is a bad
> > idea.
> [...]
> > That's why if you are using smart pointers, persist() will enter
> > the object into the session only if you pass the smart pointer:
> 
> I see. that is indeed important to note.
> 
> > If you need to convert 'this' to a shared_ptr, look into the
> > shared_from_this mechanism.
> 
> I'll try that. I'm a bit worried about the requirement for there being
> a shared_ptr to the object otherwise shared_from_this will fail. So
> how will that work with this use case?
>
> Employee e("John","Doe");
> e.persist();
>
> I'm assuming it will break?

Yes, if you are using shared_from_this then all objects should be owned
by shared_ptr. Note also that this is not really specific to ODB. If you
need more information on shared_from_this, I suggest that you consult
the documentation (Boost has a good overview) and/or general C++ mailing
lists/forums.

Boris



More information about the odb-users mailing list