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

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

    Let me give you an empirical answer: in our realtime app, we do this all the time, and it works just fine. This is the case in MSVC for Wintel and PowerPC and GCC for Linux and Mac, even for classes that have constructors.

    I can't quote chapter and verse of the C++ standard for this, just experimental evidence.

提交回复
热议问题