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

前端 未结 11 1171
灰色年华
灰色年华 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 14:04

    Your class has a constructor, and so is not POD in the sense that a C struct is. It is therefore not safe to copy it with memcpy(). If you want POD data, remove the constructor. If you want non-POD data, where controlled construction is essential, don't use memcpy() - you can't have both.

提交回复
热议问题