C memset seems to not write to every member

后端 未结 7 2197
感动是毒
感动是毒 2021-01-18 09:01

I wrote a small coordinate class to handle both int and float coordinates.

template 
class vector2
{
public:
    vector2() { memset(this, 0, s         


        
7条回答
  •  温柔的废话
    2021-01-18 09:17

    No don't use memset -- it zeroes out the size of a pointer (4 bytes on my x86 Intel machine) bytes starting at the location pointed by this. This is a bad habit: you will also zero out virtual pointers and pointers to virtual bases when using memset with a complex class. Instead do:

    template 
    class vector2
    {
    public:
        // use initializer lists
        vector2() : x(0), y(0) {}
        T x;
        T y;
    };
    

提交回复
热议问题