Why is the assignment operator not called in this case in favor of the copy constructor?

前端 未结 2 781
名媛妹妹
名媛妹妹 2020-12-20 03:09

From the wikipedia page for copy constructors:

X a = X();     

// valid given X(const X& copy_from_me) but not valid given X(X& copy_from_me)
// bec         


        
2条回答
  •  悲&欢浪女
    2020-12-20 03:56

    Because the code is constructing an object. The = sign here is initializing, not assigning. You can only assign to an existing object, not to one that's under construction.

提交回复
热议问题