Pregunta

Is there any way to implement copy constructor in C++ which only copies some specific members and implement move for other members.

For example, I have a class

class partialCopy
{
   int a;
   int largeArray[1000] ;
}

Now suppose using move constructor I want to keep only one copy of largeArray between two objects and using copy I can keep separate copy of integer a between the same two objects.

This kind of scenario may arise while coding.

Can anyone share ideas about this?

¿Fue útil?

Solución

That's a job for smart pointer:

class partialCopy {
   int a;
   std::shared_ptr<int, std::default_delete<int[]> > array;
};

This way you don't need to worry about these things - default generated special member functions will do the right thing for you.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top