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

前端 未结 11 1168
灰色年华
灰色年华 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:47

    You could. But first ask yourself:

    Why not just use the copy-constructor that is provided by your compiler to do a member-wise copy?

    Are you having specific performance problems for which you need to optimise?

    The current implementation contains all POD-types: what happens when somebody changes it?

提交回复
热议问题