[Trilinos-Users] memory copy of RCP object

Bartlett, Roscoe A. bartlettra at ornl.gov
Wed Jan 14 07:54:49 MST 2015


Sunghwan Choi,

You can only memcpy() POD (plain old data) and I can’t think of any case where you should use an RCP to manage a POD datatype.  That just does not make any sense.

RCPs are typically used to manage the lifetime of objects with reference semantics (big objects or objects with abstract interfaces).  When doing OOP, you typically don’t know the concreate type so you don’t have access to a constructor (that is the whole point of OOP).  In those cases, if you want to copy, the interface needs to support a clone() method.  The different between concreate types (i.e. using value sematics) and abstract types (i.e. using reference semantics) is explained in section 4.1 “value types vs. reference types” in:

  http://web.ornl.gov/~8vt/TeuchosMemoryManagementSAND.pdf

However, if you are using an RCP to manage an object of concrete type (call it type T) with a copy constructor, you just do:

Teuchos::RCP<T> copyMyObject(const Teuchos::RCP<const T> &t_in)
{
   return Teuchos::rcp(new T(*t_in));
}

Again, if T is a concreate type with a correct copy constructor, the above should copy the object perfectly.

If you have the time and interest to learn best practices for type manipulation and memory management in C++, I think that the document:

   http://web.ornl.gov/~8vt/TeuchosMemoryManagementSAND.pdf

is not a bad place to start, IMHO.

Cheers,

-Ross



From: trilinos-users-bounces at software.sandia.gov [mailto:trilinos-users-bounces at software.sandia.gov] On Behalf Of SungHwan Choi
Sent: Wednesday, January 14, 2015 8:39 AM
To: trilinos-users at software.sandia.gov
Subject: [Trilinos-Users] memory copy of RCP object

Dear all,
Hi,
I want to newly generate object of T class when I have RCP<T> object.
If I know the constructor of T, it is easy to make another one. However, if I don't know constructor of T or cannot access some member variables which are required to construct the object of T, how can I do? is there any simple memcopy methods?

Best
Sunghwan Choi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://software.sandia.gov/pipermail/trilinos-users/attachments/20150114/4b426aa9/attachment.html>


More information about the Trilinos-Users mailing list