C memset seems to not write to every member

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

    This might work instead:

    
    char buffer[sizeof(vector2)];
    memset(buffer, 0, sizeof(buffer));
    vector2 *v2 = new (buffer) vector2();
    

    ..or replacing/overriding vector2::new to do something like that. Still seems weird to me though.

    Definitely go with

    
    vector2(): x(0), y(0), {}
    

    0 讨论(0)
提交回复
热议问题