Can I use memcpy in C++ to copy classes that have no pointers or virtual functions

前端 未结 11 1172
灰色年华
灰色年华 2020-12-04 13:16

Say I have a class, something like the following;

class MyClass
{
public:
  MyClass();
  int a,b,c;
  double x,y,z;
};

#define  PageSize 1000000

MyClass Ar         


        
11条回答
  •  再見小時候
    2020-12-04 13:48

    I will notice that you admit there is an issue here. And you are aware of the potential drawbacks.

    My question is one of maintenance. Do you feel confident that nobody will ever include a field in this class that would botch up your great optimization ? I don't, I'm an engineer not a prophet.

    So instead of trying to improve the copy operation.... why not try to avoid it altogether ?

    Would it be possible to change the data structure used for storage to stop moving elements around... or at least not that much.

    For example, do you know of blist (the Python module). B+Tree can allow index access with performances quite similar to vectors (a bit slower, admittedly) for example, while minimizing the number of elements to shuffle around when inserting / removing.

    Instead of going in the quick and dirty, perhaps should you focus on finding a better collection ?

提交回复
热议问题