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

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

    It will work, because a (POD-) class is the same as a struct (not completely, default access ...), in C++. And you may copy a POD struct with memcpy.

    The definition of POD was no virtual functions, no constructor, deconstructor no virtual inheritance ... etc.

提交回复
热议问题