Copy constructor or = operator?

前端 未结 3 991
一整个雨季
一整个雨季 2021-01-18 04:40
    class Foo
    {


    };

    Foo f;
    Foo g = f; // (*)

My question is, what is being called in the line marked with (*) ? Is it the default

3条回答
  •  半阙折子戏
    2021-01-18 04:56

    g is actually created as a copy of f.

    A simple way to remember what = actually means, is just answer the question: is g already existing?

    {
       Foo g; //g construction ends here (at ';')
       g = f; // assignment (the g previous value is replaced)
    }
    
    {
       Foo g = f; //copy (same as Foo g(f): there is no "previous g" here)
    }
    

提交回复
热议问题