Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top