M
M
MaM2016-03-04 21:04:56
C++ / C#
MaM, 2016-03-04 21:04:56

How to create a copy of an object by pointer?

I would like to get a new object identical to the initial one, which I can change without touching the given one, the problem is that I do not know in advance the type of this object.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2016-03-04
@MaM

To get a copy of an object by its own means, you must call the copy constructor, if one is defined:

T *src = .... ;
T *dst = new T(*src);

If no copy constructor is defined, then the object must be copied manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question