C memset seems to not write to every member

后端 未结 7 2130
感动是毒
感动是毒 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:23

    dirkgently is correct. However rather that constructing x and y with 0, an explicit call to the default constructor will set intrinsic types to 0 and allow the template to be used for structs and classes with a default constructor.

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

提交回复
热议问题